- Home /
NavMeshAgent places transform incorrectly
In my code, I create enemies and NPC's, and place them at random waypoints in the level, and also assign them additional waypoints so they can wander around. I place the Transform.position, and also done NavMeshAgent.SetDestination, and tried setting NavMeshAgent.destination.
If I start it in paused mode, when the game starts I can see they're in the correct spot. But the instant I unpause the game, they are all moved towards the center of the level, and are not where they are supposed to be. Debug.Log statements also indicate they start in the correct spot.
NavNeshAgent is moving them, but not along a path, and not to where they're supposed to be. Any ideas why or what I'm doing incorrectly?
void Start()
{
// Get My Waypoints by getting all, then selecting 3.
GameObject[] allPoints = GameObject.FindGameObjectsWithTag("Waypoint");
for (int i = 0; i < 3; i++)
{
int pointNum = Random.Range(0, allPoints.Length);
Waypoints.Add(allPoints[pointNum].transform.position);
}
// Now position them at the first waypoint
index = 0;
transform.position = Waypoints[index];
navigator.destination = Waypoints[index];
// That worked, they START in the correct positions, but then NavMeshAgent teleports them to incorrect positions in the first frame.
}
After Start
After Update
Answer by Tanoshimi2000 · Feb 01, 2021 at 02:48 AM
OK, so I've figured this out for anyone who needs the answer: NavMeshAgent.Warp().
If you need to move a GameObject that has a NavMeshAgent, like with the Physics System, you cannot just set the transform.position. It confuses the NavMeshAgent.
Fortunately, NavMeshAgent.Warp(Vector3 newPosition) works the same as setting the transform.position value.