c#/Unity: Infinite Loop Issue.我疯了吗?



那么,c#新手在这里,有一点问题:

我正在尝试为我的Unity游戏编写一个boss战斗脚本,并且我正在制作它的人工智能每十秒钟,我要老板随机检查一个数字。如果它成功了,它将执行传送动画,并传送。我还没有编码传送本身,只是试图让动画触发。我希望这在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语句来检查动画状态应该没问题。

最新更新