如何在找到游戏对象之前添加延迟
我正在尝试制作一款游戏,你可以刺伤敌人,敌人挣扎大约一秒钟然后倒地死亡。 (布娃娃);
我认为最好只显示我的脚本,你知道我的意思:
在触发输入脚本中:
if(other.tag == "enemy"){
other.transform.parent.gameObject.name = ("enemy" + currentEnemy);
print(other.name);
gameObject.Find("enemy" + currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse();
BloodParticle.emit = true;
Stabbed = true;
Character.GetComponent("MouseLook").enabled = false;
Character.GetComponent("CharacterMotor").enabled = false;
}
并在更新功能中:
if(Stabbed == true){
StopBleeding ++;
}
if(StopBleeding > 50){
Stabbed = false;
StopBleeding = 0;
currentEnemy ++;
Character.GetComponent("MouseLook").enabled = true;
Character.GetComponent("CharacterMotor").enabled = true;
BloodParticle.emit = false;
}
现在,当我的刀进入敌人的碰撞时,敌人立即掉到地板上。 我尝试将:放入
gameObject.Find("enemy" + currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse();
if(StopBleeding > 50)
的更新函数中。
如果我这样做,我会收到 Null reverance 异常的错误,因为脚本可以找到敌人。虽然它可以在触发器中输入。
基本上我的问题是:有什么方法可以修复此错误,使其延迟 50 帧(StopBleeding
中的所有其余部分都有效)?
或者有什么方法可以在布偶被激活之前进行简单的延迟?
提前致谢
I am trying to make a game in where you can stab an enemy, the enemy struggles for about a second and drops dead. (ragdoll);
i think its best to just show my script and you know what I mean:
In an on trigger enter script:
if(other.tag == "enemy"){
other.transform.parent.gameObject.name = ("enemy" + currentEnemy);
print(other.name);
gameObject.Find("enemy" + currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse();
BloodParticle.emit = true;
Stabbed = true;
Character.GetComponent("MouseLook").enabled = false;
Character.GetComponent("CharacterMotor").enabled = false;
}
and in the update function:
if(Stabbed == true){
StopBleeding ++;
}
if(StopBleeding > 50){
Stabbed = false;
StopBleeding = 0;
currentEnemy ++;
Character.GetComponent("MouseLook").enabled = true;
Character.GetComponent("CharacterMotor").enabled = true;
BloodParticle.emit = false;
}
Now when my knife enters the collision of the enemy the enemy imediatly drops to the floor.
I tried putting the:
gameObject.Find("enemy" + currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse();
in the update function in if(StopBleeding > 50)
.
if I do that I get an error of Null reverance exception becaus the script cand find the enemy. While it can I its in the trigger enter.
Basicly my question is: Is there any way to fix this error to give it a 50 frame delay (all the rest in StopBleeding
works)?
Or is there any way I can put a simple delay in before the Ragdoll gets activated?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 StartCoroutine("DelayDeath");其中 DelayDeath 是方法的名称。见下文。
You can use StartCoroutine("DelayDeath"); where DelayDeath is the name of a method. See below.
由于您具体询问的是 Unity JS 而不是 C#,因此您将使用 Yield 关键字。
Since you're asking specifically about Unity JS and not C#, you'll use the yield keyword.
在刺伤时,你可以查找敌人对象,但将其保存在变量中,而不是让敌人死亡。流血结束后,你不用再去寻找敌人,就可以触发被救的敌人的死亡。
另一个提示:不要计算帧,因为帧的长度可能不同。将 Time.deltaTime 的时间相加,直到达到 2 秒,然后触发死亡。
希望有帮助。
On stabbing you could look up the enemy object but save it away in a variable instead of issuing the enemies dead. After the bleeding finishes you don't have to look up the enemy and can trigger the death on the saved one.
Another hint: Don't count frames as frames might be of different length. Add up time from Time.deltaTime until you reach let's say 2 seconds and then trigger the death.
Hope that helps.