浏览代码

Merge pull request #7195 from smix8/navigationagent_scripts_4.x

Fix NavigationAgent script examples
Max Hilbrunner 2 年之前
父节点
当前提交
b0e8d41fa4

+ 2 - 3
tutorials/navigation/navigation_introduction_2d.rst

@@ -145,15 +145,14 @@ NavigationServer2D and a NavigationAgent2D for path movement.
         if navigation_agent.is_navigation_finished():
             return
 
-        var current_agent_position: Vector2 = global_transform.origin
+        var current_agent_position: Vector2 = global_position
         var next_path_position: Vector2 = navigation_agent.get_next_path_position()
 
         var new_velocity: Vector2 = next_path_position - current_agent_position
         new_velocity = new_velocity.normalized()
         new_velocity = new_velocity * movement_speed
 
-        set_velocity(new_velocity)
-
+        velocity = new_velocity
         move_and_slide()
 
  .. code-tab:: csharp C#

+ 2 - 2
tutorials/navigation/navigation_introduction_3d.rst

@@ -150,14 +150,14 @@ a NavigationAgent3D for path movement.
         if navigation_agent.is_navigation_finished():
             return
 
-        var current_agent_position: Vector3 = global_transform.origin
+        var current_agent_position: Vector3 = global_position
         var next_path_position: Vector3 = navigation_agent.get_next_path_position()
 
         var new_velocity: Vector3 = next_path_position - current_agent_position
         new_velocity = new_velocity.normalized()
         new_velocity = new_velocity * movement_speed
 
-        set_velocity(new_velocity)
+        velocity = safe_velocity
         move_and_slide()
 
  .. code-tab:: csharp C#

+ 34 - 23
tutorials/navigation/navigation_using_navigationagents.rst

@@ -157,12 +157,14 @@ This script adds basic navigation movement to a Node3D with a NavigationAgent3D
  .. code-tab:: gdscript GDScript
 
     extends Node3D
-    # script on agent parent node, connect the agent 'velocity_computed' signal for collision avoidance
 
     @export var movement_speed: float = 4.0
     @onready var navigation_agent: NavigationAgent3D = get_node("NavigationAgent3D")
     var movement_delta: float
 
+    func _ready() -> void:
+        navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
+
     func set_movement_target(movement_target: Vector3):
         navigation_agent.set_target_position(movement_target)
 
@@ -172,13 +174,15 @@ This script adds basic navigation movement to a Node3D with a NavigationAgent3D
 
         movement_delta = movement_speed * delta
         var next_path_position: Vector3 = navigation_agent.get_next_path_position()
-        var current_agent_position: Vector3 = global_transform.origin
+        var current_agent_position: Vector3 = global_position
         var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_delta
-        navigation_agent.set_velocity(new_velocity)
+        if navigation_agent.avoidance_enabled:
+            navigation_agent.set_velocity(new_velocity)
+        else:
+            _on_velocity_computed(new_velocity)
 
-    func _on_NavigationAgent3D_velocity_computed(safe_velocity: Vector3):
-        # Move Node3D with the computed `safe_velocity` to avoid dynamic obstacles.
-        global_transform.origin = global_transform.origin.move_toward(global_transform.origin + safe_velocity, movement_delta)
+    func _on_velocity_computed(safe_velocity: Vector3) -> void:
+        global_position = global_position.move_toward(global_position + safe_velocity, movement_delta)
 
 Actor as CharacterBody3D
 ~~~~~~~~~~~~~~~~~~~~~~~~
@@ -189,11 +193,12 @@ This script adds basic navigation movement to a CharacterBody3D with a Navigatio
  .. code-tab:: gdscript GDScript
 
     extends CharacterBody3D
-    # script on agent parent node, connect the agent 'velocity_computed' signal for collision avoidance
 
     @export var movement_speed: float = 4.0
     @onready var navigation_agent: NavigationAgent3D = get_node("NavigationAgent3D")
-    var movement_delta: float
+
+    func _ready() -> void:
+        navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
 
     func set_movement_target(movement_target: Vector3):
         navigation_agent.set_target_position(movement_target)
@@ -202,14 +207,15 @@ This script adds basic navigation movement to a CharacterBody3D with a Navigatio
         if navigation_agent.is_navigation_finished():
             return
 
-        movement_delta = movement_speed * delta
         var next_path_position: Vector3 = navigation_agent.get_next_path_position()
-        var current_agent_position: Vector3 = global_transform.origin
-        var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_delta
-        navigation_agent.set_velocity(new_velocity)
-
-    func _on_NavigationAgent3D_velocity_computed(safe_velocity: Vector3):
-        # Move CharacterBody3D with the computed `safe_velocity` to avoid dynamic obstacles.
+        var current_agent_position: Vector3 = global_position
+        var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
+        if navigation_agent.avoidance_enabled:
+            navigation_agent.set_velocity(new_velocity)
+        else:
+            _on_velocity_computed(new_velocity)
+
+    func _on_velocity_computed(safe_velocity: Vector3):
         velocity = safe_velocity
         move_and_slide()
 
@@ -222,10 +228,13 @@ This script adds basic navigation movement to a RigidBody3D with a NavigationAge
  .. code-tab:: gdscript GDScript
 
     extends RigidBody3D
-    # script on agent parent node, connect the agent 'velocity_computed' signal for collision avoidance
 
+    @export var movement_speed: float = 4.0
     @onready var navigation_agent: NavigationAgent3D = get_node("NavigationAgent3D")
 
+    func _ready() -> void:
+        navigation_agent.velocity_computed.connect(Callable(_on_velocity_computed))
+
     func set_movement_target(movement_target: Vector3):
         navigation_agent.set_target_position(movement_target)
 
@@ -234,10 +243,12 @@ This script adds basic navigation movement to a RigidBody3D with a NavigationAge
             return
 
         var next_path_position: Vector3 = navigation_agent.get_next_path_position()
-        var current_agent_position: Vector3 = global_transform.origin
-        var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * velocity
-        navigation_agent.set_velocity(new_velocity)
-
-    func _on_NavigationAgent3D_velocity_computed(safe_velocity: Vector3):
-        # Move RigidBody3D with the computed `safe_velocity` to avoid dynamic obstacles.
-        set_linear_velocity(safe_velocity)
+        var current_agent_position: Vector3 = global_position
+        var new_velocity: Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
+        if navigation_agent.avoidance_enabled:
+            navigation_agent.set_velocity(new_velocity)
+        else:
+            _on_velocity_computed(new_velocity)
+
+    func _on_velocity_computed(safe_velocity: Vector3):
+        linear_velocity = safe_velocity