我需要反转这个脚本,这个脚本用来制作一个游戏对象,以便在一些转换之间进行巡逻。我需要对象从点(1,2,3,4,5)开始依次导航,当它到达数组的末尾时,它会颠倒数组本身的顺序,这样它就会向后导航(5,4,3,2 ,1)。
using UnityEngine;
using UnityEngine.AI;
public class Patrol : MonoBehaviour
{
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint()
{
if (points.Length == 0)
return;
agent.destination = points[destPoint].position;
destPoint = (destPoint + 1) % points.Length;
}
void Update()
{
if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
}
}发布于 2020-06-15 21:28:19
您应该在到达最后一点时使用Array.Reverse,以便在您的代码上轻松实现。
文档here。
将此代码添加到GoToNextPoint的末尾。
destPoint++;
if (destPoint >= points.Length)
{
Array.Reverse(points);
destPoint = 0;
}然后移除。
destPoint = (destPoint + 1) % points.Length;https://stackoverflow.com/questions/62388857
复制相似问题