From 9e0706b0878b8a4999661110f6c898d2e728279a Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Thu, 20 Apr 2023 21:42:07 +0200 Subject: [PATCH] Fix NavigationAgent script examples Fixes NavigationAgent script examples. --- .../navigation/navigation_introduction_2d.rst | 5 +- .../navigation/navigation_introduction_3d.rst | 4 +- .../navigation_using_navigationagents.rst | 53 +++++++++++-------- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/tutorials/navigation/navigation_introduction_2d.rst b/tutorials/navigation/navigation_introduction_2d.rst index acf72812c..1fb806612 100644 --- a/tutorials/navigation/navigation_introduction_2d.rst +++ b/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# diff --git a/tutorials/navigation/navigation_introduction_3d.rst b/tutorials/navigation/navigation_introduction_3d.rst index 45a9ecdcf..fc71d9a33 100644 --- a/tutorials/navigation/navigation_introduction_3d.rst +++ b/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# diff --git a/tutorials/navigation/navigation_using_navigationagents.rst b/tutorials/navigation/navigation_using_navigationagents.rst index 700626ef7..23e089414 100644 --- a/tutorials/navigation/navigation_using_navigationagents.rst +++ b/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) + 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_NavigationAgent3D_velocity_computed(safe_velocity: Vector3): - # Move CharacterBody3D with the computed `safe_velocity` to avoid dynamic obstacles. + 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) + 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_NavigationAgent3D_velocity_computed(safe_velocity: Vector3): - # Move RigidBody3D with the computed `safe_velocity` to avoid dynamic obstacles. - set_linear_velocity(safe_velocity) + func _on_velocity_computed(safe_velocity: Vector3): + linear_velocity = safe_velocity