- Home /
Navmesh After Spawn
Hey guys:
I'm creating a sidescroller game with a custom spawn system and using navmesh to guide my player. I have two scenes that loads one after another, one is vertical oriented and the other one is horizontally oriented. Here's an image that tries to explain this.
The error is this: When I go to the scene 2 and return to the scene one, player must start from scene 1 final spawn point and move to initial spawn point. So I was able to set the initial position of the player by using this code:
public void SetInitialPosition(Vector3 position)
{
agent.Stop(true);
agent.updatePosition = false;
agent.updateRotation = false;
myTransform.position = position;
}
After I use this function I call another function that executes an NavMeshAgent.Resume() so the navmesh is enabled again.
This works just fine and player is succesfully located at the correct spawn point. But when I update the code to set NavMeshAgent destination, my player is "Teleported" to the initial point of the navmesh and starts walking down from it. Do you now why is this "Teleporting" issue happening?, Is there any other function that I need to call?
Thanks a lot
Answer by kendoran · Nov 19, 2012 at 10:03 AM
It's probably too late to help but for others who had this problem, I had some luck with setting the NavMeshAgent active property to false before setting the position, then setting back to true again.
i.e.
public void SetPosition(Vector3 position)
{
_navMeshAgent.enabled = false;
transform.position = position;
_navMeshAgent.enabled = true;
}
I was having some crazy issues with the nav mesh agents and this seems to have fixed the problems so far. Thanks!!!
Answer by td-lambda · Feb 24, 2014 at 07:42 AM
Not sure this is relevant to your issue but i've experienced some odd behavior in the OnAnimatorMove method where my agents were "teleporting" at startup. The problem was that the animator was reporting a deltaPosition other than zero during the first frame setting the velocity to some random number as a result.
I ended up having to clear my velocity at startup and that seems to have fixed the problem:
void OnAnimatorMove() {
// Set the NavMeshAgent's velocity to the change in position since the last frame, by the time it took for the last frame.
Vector3 velocity = animator.deltaPosition / Mathf.Max(Time.smoothDeltaTime,0.0000000000000001f);
if(Time.frameCount < 2) velocity = Vector3.zero;
Your answer
Follow this Question
Related Questions
Navmesh with destructible obstacles 1 Answer
How to sample a point on a navMesh Agent path 0 Answers
CalculatePath often returns wrong results 0 Answers
Connecting two navmeshes without "Speedboost" 0 Answers
NavMeshAgent Not Accounting for Objects 0 Answers