Unity3D 定时发射子弹

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);
            }
        }
    }
}
Unity3D 定时发射子弹

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

Unity3D 定时发射子弹

上一篇:CentOS服务器,大访问量会造成日志文件迅速增大,半年左右得清除一下


下一篇:【SVN】删除文件/文件夹 svn: E205007: Could not use external editor to fetch log message