-Bear in mind I'm fairly inexperienced to Unity and C#-个
发生问题的脚本是一个类,用来保存动物AI的所有重要的子 routine 和函数.C#脚本的这一特定部分使动物在停止之前的一段随机时间内对随机位置进行路径查找(这是我能想到的唯一实现随机移动AI的方法,因为它利用了我已经编写的路径查找代码).它的工作方式是在离地面50个单位的随机x,z位置实例化一个空的游戏对象,并将目标位置设置为该游戏对象的位置,然后调用寻路脚本导航到该随机位置. 在FixedUpdate方法中可以找到以下脚本(FixedUpdate与更新相对,因此它的运行频率略低一些),并确保不会在每一帧发生随机移动.
int randNum = UnityEngine.Random.Range(1, 100000);
if (randNum <= 10)
{
StartCoroutine(DelayForRandomMovement());
}
我面临的问题是,Random.Range调用中的最大值randNum
、has为randNum
000,或者DelayForRandomMotion()协程中的单个重要行将不会按预期运行.此协程如下所示:
IEnumerator DelayForRandomMovement()
{
yield return new WaitForSeconds(UnityEngine.Random.Range(1f, 6f)); //wait 1-6 seconds
stop = false;
if (stop == false)
{
GameObject randomPosObj = Instantiate(randomMovementPrefab);
randomPosObj.transform.position = new Vector3(UnityEngine.Random.Range(-240, 240), 50, UnityEngine.Random.Range(-240, 240)); //instantiate empty prefab in random x and z pos within range of island size
target = randomPosObj.transform; //target = the instantiated prefab
this.gameObject.GetComponent<Rabbit>().LocateFood();
yield return new WaitForSeconds(UnityEngine.Random.Range(3f, 6f)); //wait 3-6 seconds
Destroy(randomPosObj);
}
}
没有正确执行的代码行是this.gameObject.GetComponent<Rabbit>().LocateFood()
行,我不知道为什么.该行从Rabbit类(当前类/脚本的子类)调用LocateFood()子 routine (在本例中用于通过路径查找到某个位置,而不是食物).LocateFood()子函数使用各种其他脚本进行计算,因此我将把它的内容包含在本文的最下面.
当randNum
小于this.gameObject.GetComponent<Rabbit>().LocateFood()
000时,this.gameObject.GetComponent<Rabbit>().LocateFood()
仍然会执行,但问题是这些动物根本不会寻路.我之所以知道这一点,是因为LocateFood()SUB中的Debug.Log()命令在应该执行的时候仍然会被执行.然而,当最大值设置为this.gameObject.GetComponent<Rabbit>().LocateFood()
000时,动物确实会像预期的那样寻径,并且它工作得很完美.唯一的问题是,这种情况发生得非常罕见,导致许多兔子一动不动.
我不能准确地列举我做了什么来try 和修复它,但我一遍又一遍地摆弄这部分代码,似乎什么都不起作用,所以我不得不恢复到您现在看到的状态.有没有人知道有什么方法可以重写这一点,或者给出为什么会发生这种情况的任何提示?谢谢你的帮助.
EDIT:
以下是LocateFood()
的内容:
public override void LocateFood()
{
PathRequestManager.RequestPath(transform.position, target.position, OnPathFound);
}
我知道调用RequestPath()
方法很好,因为文本如预期的那样输出到控制台. node 将被计算,其Gizmo将显示在Unity场景视图中.因此,我将只显示回调子 routine OnPathFound()
的内容:
public void OnPathFound(Vector3[] waypoints, bool pathSuccessful)
{
Debug.Log("Path found");
if (pathSuccessful)
{
path = new Path(waypoints, transform.position, turnDst, stoppingDst); ;
StopCoroutine("FollowPath");
StartCoroutine("FollowPath");
}
}
EDIT 2:个 我将粘贴当前代码以显示现在的情况:
Update():
// checks every frame to see if the timer has reached zero
if (timer <= 0)
{
if (!moving)
{
// if animal is not moving AND the timer has reached zero, determine whether movement happens based off chance
int randNum = UnityEngine.Random.Range(1, 10000);
if (randNum <= chance)
{
RandomMovement();
}
}
else //if animal is currently moving and timer reached zero
{
StopRandomMovement();
}
timer = UnityEngine.Random.Range(minInterval, maxInterval); // reset the interval timer
}
else
{
// otherwise, if the timer is greater than zero, reduce the timer by Time.deltaTime (the time in seconds since the last frame)
timer -= Time.deltaTime; // timer counts down
}
RandomMovement():
个
Debug.Log("starting wander");
target = new Vector3(UnityEngine.Random.Range(-boundSize, boundSize), height, UnityEngine.Random.Range(-boundSize, boundSize));
rabbit.LocateFood();
moving = true;
StopRandomMovement():
个
Debug.Log("stopping wander");
StopCoroutine("FollowPath");
moving = false;
我现在面临的问题是兔子不会寻路.
然而,代码似乎按预期执行:moving
布尔值经常被设置为true,几秒钟后被设置为false;正确的Debug.Log()
消息也会在正确的时刻显示在控制台中; target
Vector 3变量也会在判断器中显示适当的随机位置.This also suggests that methods running several times at once is no longer the problem.
因此,新的Update()
代码看起来像预期的那样工作,但兔子只是没有开始寻径,尽管寻径代码仍在运行,如"Path Requred"控制台输出所示(它在寻径算法的最初几个方法之一中执行).我会在不同的寻路方法/功能中再扔一些Debug.Log()
,希望能告诉我问题所在.
然而,奇怪的是,如果我在按下按钮时单独运行一次路径查找,它会完美地执行一次,并且在测试更新后的代码时,它确实工作得很好,就像我在上一条 comments 中提到的那样.自从我写了这篇文章以来,我没有对任何寻路代码做任何修改,所以我觉得奇怪的是,它只在某些时候起作用.