Unity3d 5.x 学习笔记(3)—— 发射预制体子弹(Instantiate),点射,扫射,五连发效果

来源:互联网 发布:java架构师薪资 编辑:程序博客网 时间:2024/06/04 18:31

代码实例1(点射发射一颗预制体子弹)

    using UnityEngine;    using System.Collections;    public class Shoot : MonoBehaviour {        //申请GameObject变量用来承载预制体(拖动预制体赋值)        public GameObject bullet;        //申请float变量设置推动力大小        public float thrust = 2000.0f;        //检测开火函数        void CheckShoot() {            //是否输入了“Fire1”(默认鼠标左键和左Ctrl键)            if (Input.GetButtonDown("Fire1")) {                //实例化一个bullet的克隆体。                GameObject clone = Instantiate(bullet,                                    transform.position,                                    transform.rotation)                                    as GameObject;                //获取克隆体的刚体组件。                Rigidbody rb = clone.GetComponent<Rigidbody>();                //给克隆体的刚体组件一个推动力。                rb.AddForce(clone.transform.forward * thrust);            }             }        void Update () {            CheckShoot();           }    }

代码实例2(扫射发射一颗预制体子弹)

    using UnityEngine;    using System.Collections;    public class ShootSimple : MonoBehaviour {        //申请GameObject变量用来承载预制体(拖动预制体赋值)        public GameObject bullet;        //申请float变量设置推动力大小        public float thrust = 2000.0f;        //变量设置开火速率。        public float fireRate = 0.5f;        //开火时间,初始化为零。        public float fireTime = 0.0f;        //检测开火函数        void CheckShoot() {            //是否输入了“Fire1”(默认鼠标左键和左Ctrl键) 并且 当前时间超过了开火时间            if (Input.GetButton("Fire1") && Time.time > fireTime) {                //根据开火速率调整开火时间                fireTime += fireRate;                //实例化一个bullet的克隆体。                GameObject clone = Instantiate(bullet,                                    transform.position,                                    transform.rotation)                                    as GameObject;                //获取克隆体的刚体组件。                Rigidbody rb = clone.GetComponent<Rigidbody>();                //给克隆体的刚体组件一个推动力。                rb.AddForce(clone.transform.forward * thrust);            }             }        void Update () {            CheckShoot();           }    }

代码实例3(发射五连发效果预制体子弹)

    using UnityEngine;    using System.Collections;    public class ShootSimple : MonoBehaviour {        //申请GameObject变量用来承载预制体(拖动预制体赋值)        public GameObject bullet;        //申请float变量设置推动力大小        public float thrust = 2000.0f;        //变量设置每轮之间开火间隔        public float fireRate = 1.5f;        //开火时间,初始化为零。        public float fireTime = 0.0f;        //设置一轮中子弹发射速率        public float fireWait = 0.02f;        //设置每轮子弹数量        public int bulletCount = 5;        //检测开火函数 因为函数内用了多线程,所以需要返回一个 IEnumerator 类型的值,而不是 void 。        IEnumerator CheckShoot() {            //是否输入了“Fire1”(默认鼠标左键和左Ctrl键) 并且 当前时间超过了开火时间            if (Input.GetButton("Fire1") && Time.time > fireTime) {                //根据开火速率调整开火时间                fireTime += fireRate;                //循环,发射一轮的所有子弹                for (int i = 0; i < bulletCount; i++) {                    //实例化一个bullet的克隆体。                    GameObject clone = Instantiate(bullet,                                            transform.position,                                            transform.rotation)                                            as GameObject;                    //获取克隆体的刚体组件。                    Rigidbody rb = clone.GetComponent<Rigidbody>();                    //给克隆体的刚体组件一个推动力。                    rb.AddForce(clone.transform.forward * thrust);                    //等待 fireWait 秒后发射第二课子弹                    yield return new WaitForSeconds(fireWait);                }            }             }        void Update () {            //开启协同程序            StartCoroutine(CheckShoot());               }    }
0 0