我正在进行头顶射击游戏,当玩家接触某个物体时,我就这样做了,他们的火力增加了。我怎样才能让它在几秒钟后断电?
public class PlayerPickups : MonoBehaviour {
PlayerHealth playerHealth;
public float RFBoostValue=0.005f;
GameObject player;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Rapid Fire Pickup")
{
Destroy(other.gameObject);
PlayerShooting.timeBetweenBullets -= RFBoostValue;
}
}
}
答案 0 :(得分:0)
您可以在Update()
中添加计时器并将其关闭 float timer = 0;
float seconds = 2;
private void Update() {
timer += Time.deltaTime;
if(timer > seconds) {
timer = 0;
// Do stuff
}
}
答案 1 :(得分:0)
Dond写PlayerShooting.timeBetweenBullets -= RFBoostValue;
之类的。如果您的播放器已经死亡或发生了某些事情,则数据已损坏。
你需要在播放器组件中存储一个IAffect列表或者这样的内容。 PlayerAffectController将控制每个影响生命周期。
每个影响在OnActivate方法PlayerShooting.timeBetweenBullets -= RFBoostValue;
和OnDeactivate中的PlayerShooting.timeBetweenBullets += RFBoostValue;
执行此代码。
答案 2 :(得分:0)
让我们想象一下,您触摸的位置可以让您获得通电。那时我会将一个组件附加到你的角色或某种强化管理器上,取决于你的游戏是如何构建的。 在Update方法中的powerup组件内部,你可以让你的powerup对它附加的gameobject应用它的效果,同时计算时间与user2953780的发布方式类似。时间到了,我会删除组件:
float timer = 0;
float seconds = 2;
private void Update()
{
//Apply powerup effect
//Count time
timer += Time.deltaTime;
if(timer > seconds)
{
//time to remove to powerup component
Destroy(this);
}
}