如何在找到游戏对象之前添加延迟

发布于 2024-11-02 04:41:46 字数 1361 浏览 0 评论 0原文

我正在尝试制作一款游戏,你可以刺伤敌人,敌人挣扎大约一秒钟然后倒地死亡。 (布娃娃);

我认为最好只显示我的脚本,你知道我的意思:

在触发输入脚本中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难得心□动 2024-11-09 04:41:46

您可以使用 StartCoroutine("DelayDeath");其中 DelayDeath 是方法的名称。见下文。

    if(other.tag == "enemy"){
    other.transform.parent.gameObject.name = ("enemy" + currentEnemy);
    print(other.name);
    StartCoroutine("DelayDeath");
    BloodParticle.emit = true;
    Stabbed = true;
    Character.GetComponent("MouseLook").enabled = false;
    Character.GetComponent("CharacterMotor").enabled = false;

}

private IEnumerator DelayDeath()
{
   //this will return...and one second later re-enter and finish the method
   yield return new WaitForSeconds(1.0f);
   gameObject.Find("enemy" +   currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse();
}

You can use StartCoroutine("DelayDeath"); where DelayDeath is the name of a method. See below.

    if(other.tag == "enemy"){
    other.transform.parent.gameObject.name = ("enemy" + currentEnemy);
    print(other.name);
    StartCoroutine("DelayDeath");
    BloodParticle.emit = true;
    Stabbed = true;
    Character.GetComponent("MouseLook").enabled = false;
    Character.GetComponent("CharacterMotor").enabled = false;

}

private IEnumerator DelayDeath()
{
   //this will return...and one second later re-enter and finish the method
   yield return new WaitForSeconds(1.0f);
   gameObject.Find("enemy" +   currentEnemy).GetComponent("RagdollOrNot").MakeKinematicFalse();
}
假情假意假温柔 2024-11-09 04:41:46

由于您具体询问的是 Unity JS 而不是 C#,因此您将使用 Yield 关键字。

 print("foo");
 yield WaitForSeconds (5.0); // waits for 5 seconds before executing the rest of the code
 print("bar");

Since you're asking specifically about Unity JS and not C#, you'll use the yield keyword.

 print("foo");
 yield WaitForSeconds (5.0); // waits for 5 seconds before executing the rest of the code
 print("bar");
顾忌 2024-11-09 04:41:46

在刺伤时,你可以查找敌人对象,但将其保存在变量中,而不是让敌人死亡。流血结束后,你不用再去寻找敌人,就可以触发被救的敌人的死亡。

另一个提示:不要计算帧,因为帧的长度可能不同。将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文