- Home /
Preventing a NavAgent from controlling the GameObject
So I have a system of actors in my scene, and they utilise some custom movement code. I'm attempting to allow NPC characters to utilise the NavMesh, but I want to control all the movement logic manually, rather than letting the NavAgent do it for me. This allows me to unify the way the player and NPCs move in the scene.
The way I have attempted to do this is by setting the NavAgent's speed and acceleration to 0, assigning a destination, and then using SamplePathPosition
to apply my own movement logic. This works, but occasionally my actors will move at half speed or double speed. It looks as though the NavAgent is sometimes applying speed to the actors, despite my custom settings.
It looks like the NavAgent is trying to push the characters up or through the floor for some reason, which looks to be messing with the system.
Regardless, is there a way to utilise the NavMesh for pathfinding without having the NavAgent take control of the GameObject?
Answer by Hoeloe · Feb 28, 2016 at 01:41 AM
I think I've solved this!
I needed to set these two variables:
agent.updateRotation = false;
agent.updatePosition = false;
But also, after applying my movement logic, run this line of code:
agent.nextPosition = transform.position;
This forces the agent to follow the transform rather than the other way around, so appears to do what I'm looking for.
Your answer
Follow this Question
Related Questions
NavMeshAgent's desiredVelocity and .hasPath falsely returning 0 / false 2 Answers
navmeshagent velocity is zero,but gameobject can't arrive target 0 Answers
Navmesh And Boxcolliders 0 Answers
NavMesh Agent rotates on X axis when moving and snaps back to 0. 2 Answers
Tank not moving towards player 1 Answer