You're not using Godot's Navigation correctly

The code for Godot navigation is wrong in the course. The way it’s currently coded is:

  • The AgentNode target position is set and Move () is called.
  • Move() then calculates a path to the target position by calling GetNextPathPosition(), but does nothing with the returned value. The returned value is what we want, since it’s the next point along the generated path.
  • The character velocity is then set to point to the “destination”, but the destination isn’t what we want, we want to follow the path, and so we want the value returned from the GetNextPathPosition().
  • MoveAndSlide() is then called, but since we are telling it to head in the direction of the last point in our path, all pathing is ignored and the character makes a beeline for the target.

Instead you need to use the returned value from GetNextPathPosition(). I recommend changing the move code to the following:

Vector3 nextPathPosition = characterNode.AgentNode.GetNextPathPosition();
characterNode.Velocity = characterNode.GlobalPosition.DirectionTo(nextPathPosition);
characterNode.MoveAndSlide();

1 Like

Thanks! You’re absolutely right! My character wasn’t moving and this fixed it.

Please update course to reflect this, it will end a lot of suffering for us users :sweat_smile:

at first it didn’t work for me either for some reason. this morning after i started godot it somehow just miraculously worked as intended without any changes to the original script of the course…
sometimes godot seems to work in mysterious ways

If you directly use a direction as velocity, your velocity is always of length 1. You probably want to modify (multiply) it by your character’s speed.

Also, mind that there are great code samples in the docs for different cases of what type your character’s node type is. The code also handles both cases of using or not using NavigationAgents’ avoidance capabilities.

Thanks @wkruspe for sharing the fix. It was especially helpful in my case because my path actually went through one of the invisible walls on the side of the stairs and my enemy got stuck there.

To get my patrol path working I had to 1) Move all invisible walls to be child nodes of the NavigationRegion3D and rebake the nav mesh and then 2) Use GetNextPathPosition’s return value instead of destination so that the navigation mesh is actually used to maneuver the enemy around the walls before resuming moving along the Path3D’s path.

Privacy & Terms