Я работаю в Unity 4.6.2f. Я использую Physics2D, UnityScript и A * pathfinding (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 – Leo
Игрок X: -28 Y: 1.8 Z: 0; AI X: 7,7 Y: 3.85 Z: 0 (Z всегда будет 0 couse ничего не движется в направлении Z) –