Unity 如何让一个游戏对象无休止地经历 waypoints?
Unity How to make a gameobject going through waypoints endlessly?
我有这个代码:
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class AutoPilot : MonoBehaviour {
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
void Start () {
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
GotoNextPoint();
}
void GotoNextPoint() {
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].transform.position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update () {
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 2f)
GotoNextPoint();
}
}
我通过我的waypoints的地方,agen跟着他们,但是当我在最后一个航路点时,如何让它重新启动?继续前进?
是否有可能以某种方式重置或什么是最好的方法?
我通过添加这个修复了它:
if (destPoint == points.Length) {
destPoint = 0;
}
我有这个代码:
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class AutoPilot : MonoBehaviour {
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
void Start () {
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
GotoNextPoint();
}
void GotoNextPoint() {
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].transform.position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update () {
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 2f)
GotoNextPoint();
}
}
我通过我的waypoints的地方,agen跟着他们,但是当我在最后一个航路点时,如何让它重新启动?继续前进? 是否有可能以某种方式重置或什么是最好的方法?
我通过添加这个修复了它:
if (destPoint == points.Length) {
destPoint = 0;
}