extends CharacterBody3D
@onready var nav_thingy = $NavigationAgent3D
var speed = 20.0
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
@warning_ignore("unused_parameter")
func _process(delta):
pass
@warning_ignore("unused_parameter")
func _physics_process(delta):
var curr_location = global_transform.origin
var next_location = nav_thingy.get_next_path_position()
var new_vel = (next_location - curr_location).normalized() * speed
velocity = new_vel
move_and_slide()
func update_target_location(target_location):
nav_thingy.set_target_position(target_location)
It’s not clear what you mean with flying. Remember that if the model is moved only through the agent, gravity will not affected and if there is any vertical offset will move based on the transform.
I will recommend first re bake the navigation mesh and the add some debug print when you set the target lcoation and verify the target_location is not the one setting the higher Z.
The code example you’re using is a very simplified version and is hard to catch problems. I will recommend you to use a more elaborated version to try to identify the problem.
Some examples:
1 - In the physics_process previous to anything add this check:
Do not query when the map has never synchronized and is empty.
if NavigationServer3D.map_get_iteration_id(navigation_agent.get_navigation_map()) == 0:
return
to avoid querying when the map has never synchronized and is empty.
After that you can check
if(nav_thingy.is_navigation_finished() || !nav_thingy.is_target_reachable()):
In this case you can not move.
And always check if there is any error on the console.
Recheck the navigation mesh.
Recheck the navigation agent settings, in particular the pathfinding settings.
# Vertical Velocity
if not is_on_floor(): # If in the air, fall towards the floor. Literally gravity
target_velocity.y = target_velocity.y - (fall_acceleration * delta)