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();
}
}
该脚本附加到每个代理。
我有两个特工。第一个智能体有一个航点,第二个智能体有八个航点。两个智能体在环路中的路点之间移动。我希望其中一个路点也将是它们的起始原始位置,因此每个代理也将移动到他的第一个原始起始位置作为点的一部分。
我试着在开始的时候加上这个
originalPos = transform;
points.Add(originalPos);
但这并没有改变任何事情。第一个智能体移动到他的一个航路点并停留在那里,第二个智能体在两个航路点之间循环,但没有起始位置。
发布于 2020-05-13 16:18:17
有很多原因可以避免你的代理离开它的初始位置:
1) if (!agent.pathPending && agent.remainingDistance < 1f)
此行代码指示如果靠近目的地,则转到下一个点。所以如果你的初始位置在先例航点附近,初始位置将被分流。
2)如果你的初始位置不在烘焙的路上,它将总是被分流。
因此,在使用navmesh代理时,不要忘记在每个路点之间要有相对间隔的路点。
https://stackoverflow.com/questions/61756562
复制相似问题