Unity3D 定时发射子弹

using UnityEngine;



public class example : MonoBehaviour

{

    public GameObject projectilePrefab;

    public float fireRate = 0.5F;//0.5秒实例化一个子弹

    private float nextFire = 0.0F;

    public float speed = 5f;//子弹速度



    private void Update()

    {

        if (Input.GetButton("Fire1") && Time.time > nextFire)

        {

            nextFire = Time.time + fireRate;

            Rigidbody clone = (Rigidbody)Instantiate(projectilePrefab, transform.position, transform.rotation);//实例化一个子弹预制,并转成刚体

            if (clone != null)

            {

                clone.velocity = transform.TransformDirection(Vector3.forward * speed);

            }

        }

    }

}

这样,敌人攻击时候就会0.5秒发射攻击一次了,而不会每帧都发射.

你可能感兴趣的:(unity3d)