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秒发射攻击一次了,而不会每帧都发射.

上一篇:spring + springmvc+ mybatis 事务管理及控制


下一篇:算法导论--python--插入排序