unity3d游戏开发之发射子弹的源代码.doc
上传人:qw****27 上传时间:2024-09-11 格式:DOC 页数:4 大小:491KB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

unity3d游戏开发之发射子弹的源代码.doc

unity3d游戏开发之发射子弹的源代码.doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

今天讲了发射子弹并让子弹把墙打坏,并让打出去的子弹两秒后消失效果如下:详细代码如下:usingUnityEngine;usingSystem.Collections;publicclassFire:MonoBehaviour{floatspeed=5.0f;publicGameObjectnewObject;floatfiretima=0.2f;floatnexttime=0.0f;voidUpdate(){floata=-25*Time.deltaTime;floatx=Input.GetAxis("Horizontal")*Time.deltaTime*speed;floatz=Input.GetAxis("Vertical")*Time.deltaTime*speed;transform.Translate(x,0,z);if(Input.GetKey(KeyCode.Z)){transform.Rotate(Vector3.up*a,Space.Self);}if(Input.GetKey(KeyCode.X)){transform.Rotate(Vector3.down*a,Space.Self);}if(Input.GetButton("Fire1")&&nexttime<Time.time){nexttime=firetima+Time.time;GameObjectgo=Instantiate(newObject,transform.position,transform.rotation)asGameObject;go.rigidbody.AddForce(0,0,1231);Destroy(go,2.0f);}}}接下来,我们要做一个太空大战的小游戏具体实现效果是:1、我方点击鼠标左键或按空格键发射子弹,我方子弹连续发射2、敌方飞机和子弹自动运行,子弹每隔0.5秒向我方发射一颗3、我方飞机移动时,敌方子弹跟随我方飞机移动目前实现第一步代码部分代码如下:usingUnityEngine;usingSystem.Collections;publicclassPlayer:MonoBehaviour{floatspeed=-50.0f;publicGameObjectMyplayer;voidUpdate(){floata=Time.deltaTime;floatx=Input.GetAxis("Horizontal")*Time.deltaTime*-speed;floatz=Input.GetAxis("Vertical")*Time.deltaTime*-speed;transform.Translate(x,0,z);if(Input.GetButton("Fire1")){//nexttime=firetima+Time.time;GameObjectgo=Instantiate(Myplayer,transform.position,transform.rotation)asGameObject;go.rigidbody.AddForce(0,0,1231);Destroy(go,2.0f);}}}把脚本放到Player身上,(记得给子弹天加刚体),把预设体拖到这篇文章来自HYPERLINK"http://www.gopedu.com/"狗刨学习网