From 2dc300a0fc1741ac7d3ad0583c224842aff91632 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 2 Sep 2023 12:47:13 +0200 Subject: [PATCH] Backported from godot4: Stops NavigationAgents moving to the world origin without anyone telling them to do so. -smix8 https://github.com/godotengine/godot/commit/860379fc161698234b2757c6f62f8e05fa68ada8 --- scene/2d/navigation_agent_2d.cpp | 8 +++++++- scene/2d/navigation_agent_2d.h | 4 ++-- scene/3d/navigation_agent.cpp | 6 ++++++ scene/3d/navigation_agent.h | 3 +-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/scene/2d/navigation_agent_2d.cpp b/scene/2d/navigation_agent_2d.cpp index 6ce89546a..1b3d15f18 100644 --- a/scene/2d/navigation_agent_2d.cpp +++ b/scene/2d/navigation_agent_2d.cpp @@ -248,7 +248,7 @@ void NavigationAgent2D::_notification(int p_what) { #endif // DEBUG_ENABLED } break; case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { - if (agent_parent) { + if (agent_parent && target_position_submitted) { if (velocity_submitted) { velocity_submitted = false; if (avoidance_enabled) { @@ -573,6 +573,7 @@ real_t NavigationAgent2D::get_path_max_distance() { void NavigationAgent2D::set_target_position(Vector2 p_position) { target_position = p_position; + target_position_submitted = true; _request_repath(); } @@ -663,6 +664,10 @@ void NavigationAgent2D::update_navigation() { return; } + if (!target_position_submitted) { + return; + } + update_frame_id = Engine::get_singleton()->get_physics_frames(); Vector2 origin = agent_parent->get_global_transform().get_origin(); @@ -788,6 +793,7 @@ void NavigationAgent2D::update_navigation() { _check_distance_to_target(); nav_path_index -= 1; navigation_finished = true; + target_position_submitted = false; emit_signal("navigation_finished"); break; } diff --git a/scene/2d/navigation_agent_2d.h b/scene/2d/navigation_agent_2d.h index f04accea2..7a856c348 100644 --- a/scene/2d/navigation_agent_2d.h +++ b/scene/2d/navigation_agent_2d.h @@ -66,6 +66,8 @@ class NavigationAgent2D : public Node { real_t path_max_distance; Vector2 target_position; + bool target_position_submitted; + Ref navigation_query; Ref navigation_result; int nav_path_index; @@ -84,8 +86,6 @@ class NavigationAgent2D : public Node { Vector2 velocity_forced; bool velocity_forced_submitted; - bool target_position_submitted; - bool target_reached; bool navigation_finished; // No initialized on purpose diff --git a/scene/3d/navigation_agent.cpp b/scene/3d/navigation_agent.cpp index 6e82b3fed..55bbfd4a9 100644 --- a/scene/3d/navigation_agent.cpp +++ b/scene/3d/navigation_agent.cpp @@ -550,6 +550,7 @@ real_t NavigationAgent::get_path_max_distance() { void NavigationAgent::set_target_position(Vector3 p_position) { target_position = p_position; + target_position_submitted = true; _request_repath(); } @@ -648,6 +649,10 @@ void NavigationAgent::update_navigation() { return; } + if (!target_position_submitted) { + return; + } + if (update_frame_id == Engine::get_singleton()->get_physics_frames()) { return; } @@ -773,6 +778,7 @@ void NavigationAgent::update_navigation() { _check_distance_to_target(); nav_path_index -= 1; navigation_finished = true; + target_position_submitted = false; emit_signal("navigation_finished"); break; } diff --git a/scene/3d/navigation_agent.h b/scene/3d/navigation_agent.h index d8e7ca883..7ec04d66a 100644 --- a/scene/3d/navigation_agent.h +++ b/scene/3d/navigation_agent.h @@ -72,6 +72,7 @@ class NavigationAgent : public Node { real_t path_max_distance; Vector3 target_position; + bool target_position_submitted; Ref navigation_query; Ref navigation_result; @@ -96,8 +97,6 @@ class NavigationAgent : public Node { // While not perfect it at least looks way better than agent's that clip through everything that is not a flat surface float stored_y_velocity; - bool target_position_submitted; - bool target_reached; bool navigation_finished; // No initialized on purpose