Browse Source

Merge pull request #11089 from dmlary/navagent-node3d-bouce-fix

fix NavigationAgent3D template for Node3D
Max Hilbrunner 3 weeks ago
parent
commit
94cc17d56b
1 changed files with 5 additions and 4 deletions
  1. 5 4
      tutorials/navigation/navigation_using_navigationagents.rst

+ 5 - 4
tutorials/navigation/navigation_using_navigationagents.rst

@@ -505,7 +505,7 @@ The following sections provides script templates for nodes commonly used with Na
 
 
             @export var movement_speed: float = 4.0
             @export var movement_speed: float = 4.0
             @onready var navigation_agent: NavigationAgent3D = get_node("NavigationAgent3D")
             @onready var navigation_agent: NavigationAgent3D = get_node("NavigationAgent3D")
-            var movement_delta: float
+            var physics_delta: float
 
 
             func _ready() -> void:
             func _ready() -> void:
                 navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
                 navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
@@ -514,22 +514,23 @@ The following sections provides script templates for nodes commonly used with Na
                 navigation_agent.set_target_position(movement_target)
                 navigation_agent.set_target_position(movement_target)
 
 
             func _physics_process(delta):
             func _physics_process(delta):
+                # Save the delta for use in _on_velocity_computed.
+                physics_delta = delta
                 # Do not query when the map has never synchronized and is empty.
                 # Do not query when the map has never synchronized and is empty.
                 if NavigationServer3D.map_get_iteration_id(navigation_agent.get_navigation_map()) == 0:
                 if NavigationServer3D.map_get_iteration_id(navigation_agent.get_navigation_map()) == 0:
                     return
                     return
                 if navigation_agent.is_navigation_finished():
                 if navigation_agent.is_navigation_finished():
                     return
                     return
 
 
-                movement_delta = movement_speed * delta
                 var next_path_position: Vector3 = navigation_agent.get_next_path_position()
                 var next_path_position: Vector3 = navigation_agent.get_next_path_position()
-                var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_delta
+                var new_velocity: Vector3 = global_position.direction_to(next_path_position) * movement_speed
                 if navigation_agent.avoidance_enabled:
                 if navigation_agent.avoidance_enabled:
                     navigation_agent.set_velocity(new_velocity)
                     navigation_agent.set_velocity(new_velocity)
                 else:
                 else:
                     _on_velocity_computed(new_velocity)
                     _on_velocity_computed(new_velocity)
 
 
             func _on_velocity_computed(safe_velocity: Vector3) -> void:
             func _on_velocity_computed(safe_velocity: Vector3) -> void:
-                global_position = global_position.move_toward(global_position + safe_velocity, movement_delta)
+                global_position = global_position.move_toward(global_position + safe_velocity, physics_delta * movement_speed)
 
 
          .. code-tab:: gdscript CharacterBody3D
          .. code-tab:: gdscript CharacterBody3D