接下来,我们要使飞船能发射子弹,所以我们要创建子弹模型,在资源中找到rocket.fbx模型文件,拖到Hierarchy中,这样就创建了子弹的游戏体,当然对于重复使用的游戏体,我们要做成预设体Prefab,我们打开菜单栏Assets->Create->Prefab,来创建一个新的预设体,如下图:
![](https://img-blog.csdn.net/20140721150210055?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva3Vsb3ZleW91d2Vp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
我们命名为Rocket,
![](https://img-blog.csdn.net/20140721150423747?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva3Vsb3ZleW91d2Vp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
那么子弹,飞船要发射,所以我们新建一个cs脚本,命名为Rocket,代码如下:
- using UnityEngine;
- using System.Collections;
-
- [AddComponentMenu("MyGame/Rocket")]
-
-
- public class Rocket : MonoBehaviour {
-
-
- public float m_speed=10;
-
-
- public float m_liveTime=1;
-
-
- public float m_power=1.0f;
-
-
- protected Transform m_transform;
-
-
-
- void Start ()
- {
-
-
- m_transform=this.transform;
-
-
- }
-
-
- void Update ()
- {
-
- m_liveTime-=Time.deltaTime;
-
-
- if(m_liveTime<=0)
- {
-
-
- Destroy(this.gameObject);
-
- }
-
- this.m_transform.Translate(new Vector3(0,0,-m_speed*Time.deltaTime));
-
- }
-
- }
把子弹脚本挂到子弹prefab上,然后我们再修改Player脚本,当我们按下空格或是鼠标左键的时候,就发射子弹,代码如下: