International Edition
Latest News
Entertainment

Godot 4.3 Top-Down 2D: Cancel Click-to-Move Pathfinding with Keyboard Input Using a Three-State Controller

A technical guide published by A5数据 details how developers can make keyboard movement interrupt click-to-move pathfinding in Godot 4.3 top-down 2D games, preventing characters from resuming stale navigation targets after directional keys are released. The solution centers on…

Godot 4.3 Top-Down 2D: Cancel Click-to-Move Pathfinding with Keyboard Input Using a Three-State Controller

A technical guide published by A5数据 details how developers can make keyboard movement interrupt click-to-move pathfinding in Godot 4.3 top-down 2D games, preventing characters from resuming stale navigation targets after directional keys are released.

The solution centers on a three-state controller — IDLE, MANUAL, and PATH — that funnels all velocity decisions through a single source each physics frame, according to A5Idc.

When directional input is non-zero, the controller cancels any pending click target and switches to manual mode; when input returns to zero without a new click, the character stops instead of resuming the previous path. A new click is required to re-enter pathfinding mode.

“The key is not to set the navigation target to the character’s feet when a key is pressed, but to ensure that keyboard input and pathfinding results go through the same velocity decision point,” the guide says, emphasizing that the fix lies in unifying the velocity decision point rather than manipulating the navigation target.

The approach also discards pending clicks during keyboard input, ensuring a click made while holding a direction key does not queue up for execution upon release.

The sample implementation uses a CharacterBody2D with a NavigationAgent2D child node, GDScript, and a standard 2D viewport. Input events are handled in _unhandled_input, which records click positions only when no directional input is active.

The _physics_process function reads direction first; if present, it clears the pending click flag, sets the mode to MANUAL, and applies manual speed. Otherwise, it processes pending clicks or follows the existing path.

Developers are advised to verify navigation map synchronization using map_get_iteration_id() before submitting targets, as a newly entered scene may not have completed its first map sync. The guide also distinguishes between an unreachable target and a completed navigation, noting that is_navigation_finished() signals the end of path following, not necessarily arrival at the clicked coordinate.

For projects using local avoidance, the guide recommends selecting the desired velocity — manual, path, or zero — before passing it to the agent, avoiding duplicate movement pipelines. A validation checklist includes a wall-circumnavigation test: the character should switch to manual movement immediately when a direction key is pressed mid-path, stop on release, and only resume pathfinding after a fresh click.

About the author: Entertainment Desk

Entertainment Desk at Archynewsy.