【问题标题】:AI faces Weird Direction when following a waypoint UNITYAI 在跟随航路点 UNITY 时面临奇怪的方向
【发布时间】:2017-01-25 16:05:07
【问题描述】:

我的问题是,每当它试图向当前航路点移动时,它都会面临奇怪的方向并且没有完全遵循路径。

IEnumerator FollowPath() {
    Vector3 currentWaypoint = path[0];

    while (true) {
        //transform.LookAt (currentWaypoint);
        if (transform.position == currentWaypoint) {

            PathRequestManager.RequestPath(transform.position,target.position,OnPathFound);
            targetIndex=0;
            targetIndex ++;
            //Debug.Log(currentWaypoint);

            if (targetIndex >= path.Length) {
                targetIndex =0;
                path = new Vector3[0];
                //yield break;
            }
            currentWaypoint = path[targetIndex];
        }

        transform.LookAt (currentWaypoint);
        transform.position = Vector3.MoveTowards(transform.position,currentWaypoint,speed * Time.deltaTime);

        yield return null;
    }
}  

目前正在使用 * 寻路。完整的源代码在这里:https://github.com/SebLague/Pathfinding

到达当前航点时,随机改变方向或随机朝向,不经过下一个航点。 (Screenshot)

【问题讨论】:

    标签: c# unity3d artificial-intelligence path-finding a-star


    【解决方案1】:

    在代码的这一部分,targetIndex 将始终计算为 1

    targetIndex=0;
    targetIndex ++;
    

    所以基本上,它永远不会比第一个航路点更远。

    另外,我会建议,而不是

    if (transform.position == currentWaypoint)
    

    你可以这样做:

    float threshold = 0.1f;                // Adjust to your preference
    if ((currentWaypoint - transform.position).sqrMagnitude < threshold)
    

    这是因为我认为MoveTowards 可能会过冲或无法准确到达目标矢量,并且在悬停在目标矢量上时会转身并剧烈改变方向。

    【讨论】:

    • 我忘了告诉它在没有方向或位置是静止的时候确实会去另一个航点。但是当我也向他注视的地方添加方向时,它面对奇怪的方向大约 10 秒,然后转到下一个航路点,或者有时不去下一个航路点,因为他正盯着那个当前航路点。
    • 摆脱targetIndex=0;
    • 我确实尝试了您的建议,但到达航点后它仍然面临着奇怪的方向。虽然我以某种方式得到了错误的来源。它来自动画和这行代码“transform.LookAt(currentWaypoint);”。我确实尝试删除动画,并以某种方式转到下一个航点。我怎样才能解决这个问题?动画只有静态运行。所以它没有空闲或任何东西,只有运行动画。
    • 我一定会尝试你的建议。谢谢您的帮助!如果可行,稍后会更新您。
    • 效果很好!谢谢。那救了我! ?
    猜你喜欢
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多