0

I've got a turn based game which switches between units being NavMeshAgents (when selected) and NavMeshObjects (when not selected)

I'm just using NavMeshAgent to navigate the path which I then navigate manually so I have updatePosition & updateRotation permanently set to off

When I select the agent if I check the nextPosition & destination of the agent just before enabling the agent they are both the same as my gameObject but as soon as enable the agent the nextPos & dest both jump by approx 2 on the z axis.

Weirdly I can't then change the nextPosition or destination to the correct value manually by setting them directly or through the SetDestination or Warp functions. So I end up with the navigational path starting in the wrong place.

Code below with comments describing what I'm seeing, I'm currently on unity version 2020.2.7f1 but have tried a few 2.x versions

    protected void SetCharacterNavAgentOn()
    {
        obstacle.enabled = false; // nextposition & destination are correct at this point
        agent.enabled = true; // but now they have been offset
        agent.nextPosition = gameObject.GetComponent<Rigidbody>().position; // this doesn't change the nextPosition
        agent.SetDestination(gameObject.GetComponent<Rigidbody>().position); // this doesn't change the destination
        bool isSuccessfull = agent.Warp(gameObject.GetComponent<Rigidbody>().position); // this doesn't change the nextPos or destination despite returning true
        gameObject.GetComponent<BoxCollider>().enabled = false;
        gameObject.GetComponent<Rigidbody>().isKinematic = true;
    }

Any help would be really appreciated, possibly a bug but I've dug around and haven't been able to find any examples of others experiencing the same issue.

Ben S
  • 1
  • 1

1 Answers1

0

Find the answer in this thread for anyone else who is looking https://forum.unity.com/threads/disabling-navmesh-obstacle-and-enabling-navmesh-agent-on-unit-causes-jumping-before-moving.1010998/

Basically it's a timing things, needs time to re-bake the mesh before enabling the Agent.

Ben S
  • 1
  • 1