繁体   English   中英

c#/Unity:无限循环问题...我要疯了吗?

[英]c#/Unity: Infinite Loop Issue... Am I going crazy?

所以,这里的 c# noob 有点问题:

我正在尝试为我的 Unity 游戏编写一场 Boss 战,并且我正在让它成为 AI 每 10 秒,我希望老板检查一个随机数。 如果成功,它将执行传送动画,然后传送。 我没有对传送本身进行编码,只是试图让动画触发。 我希望这在整个boss战中一直持续下去,直到boss被击败。

不幸的是,这是一个无限循环,每次我运行它时都会使我的 Unity 崩溃。 我知道在 Update() 中使用它是一个愚蠢的想法,但我尝试了很多东西却一无所获。 我在这里疯了! 我错过了什么明显的东西吗?

无论如何,这是代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmellseerAI : MonoBehaviour
{
    public Animator animator;
    public SmellseerHealth smellseerhealth;
    public GameObject tele1;
    public GameObject tele2;
    public GameObject tele3;
    public DateTime TimeOfLastTeleport;
    private bool teleporting = false;
    void Start()
    {
        TimeOfLastTeleport = System.DateTime.Now;

    }
    void Update()
    {

        Debug.Log("Starting teleportcheck!");
        int TeleportMin = 1;
        int TeleportMax = 10;
        int RandomTeleport = UnityEngine.Random.Range(TeleportMin, TeleportMax);
        var diffInSeconds = (System.DateTime.Now - TimeOfLastTeleport).TotalSeconds;

        Debug.Log("diffInSeconds is " + diffInSeconds);
        if ((RandomTeleport > 5) && (diffInSeconds > 3))
        {
            Debug.Log("Teleporting!");

            teleporting = true;
            animator.SetBool("teleporting", true);
            while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1)
            {  //If normalizedTime is 0 to 1 means animation is playing, if greater than 1 means finished
                Debug.Log("anim playing");
            }
            animator.SetBool("teleporting", false);
            TimeOfLastTeleport = System.DateTime.Now;
        }
        else
        {
            Debug.Log("Failed to teleport");
        }
        Debug.Log("Gaming!");

    }
}

您正在Update()内运行 while 循环:

while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1)

但是动画师的时间并没有改变,因为它会更新每一帧,并且您会导致主线程与您的 while 循环一起挂起。 在您的 while 循环中断之前,您不会释放 Update() (因此不允许绘制下一帧),但是您的 while 循环的条件需要更多帧。 所以你只能等到永远。

尝试将你的传送值移动到类中,在Start()中初始化它们,然后在完成所有检查后再次重新设置它们。 然后你可以返回你的条件不满足的每一帧。 考虑以下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmellseerAI : MonoBehaviour
{
    public Animator animator;
    public SmellseerHealth smellseerhealth;
    public GameObject tele1;
    public GameObject tele2;
    public GameObject tele3;
    public DateTime TimeOfLastTeleport;
    private bool teleporting = false;
    
    // NEW
    int TeleportMin = 1;
    int TeleportMax = 10;
    int RandomTeleport;

    void Start()
    {
        TimeOfLastTeleport = System.DateTime.Now;
        //NEW
        RandomTeleport = UnityEngine.Random.Range(TeleportMin, TeleportMax);    
    }
    void Update()
    {
        var diffInSeconds = (System.DateTime.Now - TimeOfLastTeleport).TotalSeconds;

        Debug.Log("diffInSeconds is " + diffInSeconds);
        if ((RandomTeleport > 5) && (diffInSeconds > 3))
        {
            Debug.Log("Teleporting!");

            teleporting = true;
            animator.SetBool("teleporting", true);
            // NEW
            if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1)
            {  //If normalizedTime is 0 to 1 means animation is playing, if     greater than 1 means finished
                Debug.Log("anim playing");
                // NEW
                return;
            }
            animator.SetBool("teleporting", false);
            TimeOfLastTeleport = System.DateTime.Now;
        }
        else
        {
            Debug.Log("Failed to teleport");
        }
        Debug.Log("Gaming!");
        
        // NEW
        RandomTeleport = UnityEngine.Random.Range(TeleportMin, TeleportMax);    
}
}

当您启动动画时,您的代码进入 while 循环并且永远不会完成,unity 会等到所有脚本完成后再渲染下一帧,这意味着动画永远不会完成

删除 while 循环并使其成为 if 语句来检查动画状态应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM