【发布时间】:2020-10-04 21:39:39
【问题描述】:
我需要反转这个用于制作游戏对象在一些变换之间巡逻的脚本。我需要对象从点 (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();
}
}
【问题讨论】:
-
将您的 +1 "
(destPoint + 1)" 转换为变量(desPoint + x)。声明x = 1;然后在分配你的 agent.destination 之前做if (points.Length == 0) { x = x * -1}