【问题标题】:Enemy is not following path敌人不遵循路径
【发布时间】:2015-04-21 21:56:27
【问题描述】:

我正在使用 Unity 4.6.2f。我正在使用 Physics2D、UnityScript 和 A* 寻路 (http://arongranberg.com/astar/) 免费版 3.6。我的问题是用我的 EnemyAI 脚本移动敌人,它在 gizmo 中正确显示了路径,但我的敌人没有跟随它。看起来他总是试图到达路径中的第 0 个点,它只是在空中飞来飞去。

EnemyAI 脚本 - 我认为问题可能出在最后 4 行代码中:

import Pathfinding;
@script RequireComponent(Seeker)
@script RequireComponent(Rigidbody2D)

var seeker : Seeker;
var rb : Rigidbody2D;
var path : Path;
var target : Transform;
var updateRate : float = 2f;
var currentWaypoint : int = 0;
var nextWaypointDistance : float;
var pathIsEnded :boolean;
var speed : float = 300f;
var fMode : ForceMode2D;

function Start () {
    rb = GetComponent.<Rigidbody2D>();
    seeker = GetComponent.<Seeker>();
    if (target == null)Debug.LogError("!!!NO PLAYER FOUND!!!");
    //Start the path
    seeker.StartPath(transform.position, target.position,OnPathComplete);
    StartCoroutine(UpdatePath());               
}
//function for finding new way when target moves
function UpdatePath() :IEnumerator {
    if (target == null) {
            //TODO: Insert a player search here.
            return;
        }
        seeker.StartPath (transform.position, target.position, OnPathComplete); 
        yield WaitForSeconds( 1f/updateRate );
        StartCoroutine(UpdatePath());
    }

function OnPathComplete (p:Path) {
        Debug.Log ("We got a path. Did it have an error? " + p.error);
        if (!p.error) {
            path = p;
            currentWaypoint = 0;
        }
}

function FixedUpdate(){
        if (target == null) {
            //TODO: Insert a player search here.
            return;
        }

        //TODO: Always look at player?

        if (path == null)
            return;

        if (currentWaypoint >= path.vectorPath.Count) {
            if (pathIsEnded)
                return;

            Debug.Log ("End of path reached.");
            pathIsEnded = true;
            return;
        }
        pathIsEnded = false;

        //Direction to the next waypoint
        var dir: Vector3 = (path.vectorPath[currentWaypoint] - transform.position).normalized;
        dir *= speed * Time.fixedDeltaTime;

        //Move the AI
        rb.AddForce (dir, fMode);
        Debug.Log(dir);

        var dist:float = Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]);
        if (dist < nextWaypointDistance) {
            currentWaypoint++;
            return;
        }
}

【问题讨论】:

  • 执行开始时玩家和AI的位置是什么?不只是 X 和 Y。它们三个:X、Y、Z
  • 玩家 X:-28 Y:1.8 Z:0 ; AI X:7,7 Y:3.85 Z:0(Z 将始终为 0,因为 Z 方向没有任何移动)

标签: unity3d 2d unityscript path-finding


【解决方案1】:

确实,问题似乎出在最后 4 行。这里有几件事可能是错误的:

  • 当您使用 2D 坐标时,您正在使用 Vector3 计算与 AI 和目标航路点的距离。确保这两个项目在同一个 Z 坐标中工作,或者仅使用 X 和 Y 坐标计算距离。
  • 未设置 NextWaypointDistance。如果该值是一个随机数并且是一个负数,那么玩家将永远不会“到达”该节点。

始终检查您的变量是否已初始化,并且在处理 2D 时,不要只忽略 Z 坐标,它有它的用途,它还可能会弄乱您的距离计算。

【讨论】:

  • 刚刚看到您关于 Z 坐标的评论。那么这不是你的问题。如果有人遇到类似问题,我会将其留在答案中,这对他们来说可能是正确的。
猜你喜欢
  • 1970-01-01
  • 2019-02-22
  • 2012-07-10
  • 2016-03-18
  • 2020-09-24
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多