好的,所以我一直试图在Unity中创建一个字符(多个)函数,等待字符到达具有while()
循环的建筑物,但它并不容易…
所以…这是我得到的:
public async void ExecuteTask(string task)
{
Debug.Log($"{this.FullName} is doing {task}");
switch (task)
{
case "Sleep":
if (this.CurrentLocation != this.Home.name) await GoTo(Home);
break;
case "Home":
if (this.CurrentLocation != this.Home.name) await GoTo(Home);
break;
case "Work":
if (this.CurrentLocation != this.Work.name) await GoTo(Work);
break;
default:
Debug.LogWarning($"Pawn.cs > {this.FullName} caught out of bound task: {task}");
break;
}
}
private Task GoTo(GameObject building)
{
this.CurrentLocation = "World";
Debug.Log($"Pawn.cs > {this.FullName}: Going to {building.name}");
Vector3 target = building.GetComponent<Building>().EntryNode.transform.position;
NavMeshAgent agent = this.GetComponent<NavMeshAgent>();
agent.destination = target;
while (agent.hasPath) ;
Debug.Log($"Pawn.cs > {this.FullName} has arrived at {building.name}");
this.CurrentLocation = building.name;
return Task.CompletedTask;
}
这是非常直接的,它得到一个任务,每小时我给ExecuteTask()
,可悲的是Unity崩溃时,我运行这段代码,问题的while()
循环,但我不知道我做错了什么?我执行任务错了吗?我可以不这样用吗?为什么?我怎么解决这个问题?
while(agent.hasPath);
等价于下面,所以它将是一个无限循环。
请删除此代码。
while(true)
{
}