如果玩家处于尖刺状态,如何每秒秒后对其造成伤害? [英] How to Damage Player after each Second if he is on Spikes?
问题描述
我得到了尖刺和bool onSpikes
,它告诉玩家是否为onSpikes.
当前,如果玩家启用了秒杀,则生命值会持续下降.
I got Spikes and the bool onSpikes
, wich tells if the player is onSpikes or not.
Currently the health goes Down constantly, if the player is onSpikes.
if(onSpikes)
{
health -= 0.01f;
}
但是我认为这不是一个好的解决方案,因为我处于无效的Update()中,并且我无论如何都希望Health丢弃更大的块(例如每秒-0.2f).
But I think this it isn't a good solution, cause I'm in void Update(), and I anyways wanted the Health to drop in bigger pieces ( like -0.2f each second).
我已经尝试构建方法并使用Invoke,但是像这样,我的播放器并没有失去任何健康. 代码:
I've already tried building an Method and use Invoke, but like this my player dind't lost health at all. Code:
private void Update(){
Debug.Log("Player on Spikes: "+SpikeDMG.onSpikes);
bar.localScale = new Vector3(health,1f);
if(SpikeDMG.onSpikes)
{
Invoke("doDmg",1);
}
//Wenn leben unter 0 fällt ==> starte szene neu
if(health <=0){
int scene = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(scene, LoadSceneMode.Single);
Time.timeScale = 1;
SpikeDMG.onSpikes = false;
}
}
void doDmg(){
health -= 10/100;
}
}
感谢所有能提供帮助的人!
Thanks to everyone who can help!
推荐答案
要执行的下一个动作是使用某种时间戳.还此处进行了描述,并此处.>
What you want to do is use some kind of time stamp, when your next action should be executed. It is also described here and here.
private float spikeDamageAt= 0.0f;
void update() {
if(onSpikes && Time.time >= spikeDamageAt) {
health -= 0.01f;
spikeDamageAt= Time.time + 1.0f; // for 1 seconds
}
}
这篇关于如果玩家处于尖刺状态,如何每秒秒后对其造成伤害?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!