您的位置:首页 > 移动开发 > Unity3D

Unity3D 之武器系统冷却功能的实现方式

2016-06-04 18:18 357 查看
先上方法

//如果Fire1按钮被按下(默认为ctrl),每0.5秒实例化一发子弹

public GameObject projectile;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
void Update() {
if (Input.GetButton("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
duck clone = Instantiate(projectile, transform.position, transform.rotation);
}
}


还有一个简答的方法就是使用MonoBehaviour.InvokeRepeating 重复调用:

在2S后,每隔0.5s进行子弹的实例化。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public GameObject projectile;
public void Awake() {
InvokeRepeating("Weapon", 2, 0.5F);
}

void Weapon() {
duck clone = Instantiate(projectile, transform.position, transform.rotation);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: