我怎样才能让每个代理也回到原来的位置?


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AgentControl : MonoBehaviour
{
public List<Transform> points;
private int destPoint = 0;
private NavMeshAgent agent;
private Transform originalPos;
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).
agent.autoBraking = false;
originalPos = transform;
points.Add(originalPos);
GotoNextPoint();
}

void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Count == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Count;
}

void Update()
{
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 1f)
GotoNextPoint();
}
}

脚本将附加到每个代理。

我有 2 个代理。第一个代理有一个航点,第二个代理有八个航点。 两个代理在航点之间循环移动。 我希望其中一个航点也将是它们的起始原始位置,以便每个代理也将移动到他的第一个原始起始位置作为点的一部分。

我试图在开始中添加它

originalPos = transform;
points.Add(originalPos);

但这并没有改变任何东西。第一个代理移动到他的一个航点并停留在那里,第二个代理在航点之间循环,但没有起始位置。

很多原因可能会避免您的代理走上其初始位置:

1(if (!agent.pathPending && agent.remainingDistance < 1f)这行代码指示如果靠近目的地,则转到下一个点。因此,如果您的初始位置接近先行点,则初始位置将被分流......

2(如果您的初始位置不在烘烤途中,它将始终被分流..

因此,在使用导航网格代理时,不要忘记在每个航点之间保持相对间隔

最新更新