【发布时间】:2020-07-28 15:18:41
【问题描述】:
问题是,我正在使用我在网上找到的教程进行寻路,并且航点正在添加到协程中,我需要访问协程中定义的变量“currentwaypoint”
public IEnumerator FollowPath()
{
Vector3 currentWayPoint = path[0];
while (true)
{
if (transform.position == currentWayPoint)
{
targetIndex++;
if (targetIndex >= path.Length)
{
targetIndex = 0;
path = new Vector3[0];
reached = true;
yield break;
}
reached = false;
currentWayPoint = path[targetIndex];
lastDir = (currentWayPoint - transform.position).normalized;
}
transform.position = Vector3.MoveTowards(transform.position, currentWayPoint, speed * Time.deltaTime);
transform.rotation = Quaternion.Euler(0, 0, GetAngle(currentWayPoint));
fov.SetAngle(GetAngle(currentWayPoint));
yield return null;
}
但是当我添加在协程外部定义的“lastDir”变量时,它只返回 0,0,0,这是我猜的默认值。
所以我需要的是在循环中更新时访问这个变量值
提前致谢
【问题讨论】:
-
可以发完整的脚本吗?
标签: c# unity3d coroutine path-finding game-development