我不知道这是问这个问题的正确地方。因为这是一个游戏开发相关的问题。我在这里发布了这个问题(gamedev.stackexchange.com - https://gamedev.stackexchange.com/questions/109190/navmashagent-not-moving)但仍然没有回应。并且还在 Unity 论坛 ( http://answers.unity3d.com/questions/1075904/navmashagent-not-moving-in-unity3d-5.html ) 上发帖。任何人来帮助我。
我会再次在这里发布我的问题
我试图将多维数据集移动到检查点。FindClosestTarget() 方法给出最近的检查点。当 Cube 击中 CheckPoint 时,ChackPoint 将被设置为非活动状态。当发生碰撞时,再次调用 FindClosestTarget() 并获取新的最近检查点。事情是立方体不动。它看着最近的检查点但不动。
using UnityEngine;
using System.Collections;
public class EnimyAI03N : MonoBehaviour {
NavMeshAgent agent;
Transform Target;
private float rotationSpeed=15.0f;
void Start () {
agent = GetComponent<NavMeshAgent>();
Target = FindClosestTarget ().transform;
}
void Update ()
{
LookAt (Target);
MoveTo (Target);
}
// Turn to face the player.
void LookAt (Transform T){
// Rotate to look at player.
Quaternion rotation= Quaternion.LookRotation(T.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*rotationSpeed);
//transform.LookAt(Target); alternate way to track player replaces both lines above.
}
void MoveTo (Transform T){
agent.SetDestination(T.position);
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name.Contains("CheckPoint"))
{
Target = FindClosestTarget ().transform;
}
}
GameObject FindClosestTarget() {
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("CheckPoint");
GameObject closest=null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos) {
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
}