When one frame's step is longer than the distance left, the guard steps past the point, turns around and steps past it again. If neither side ever lands within your arrival distance, it shakes there forever. In our Godot 4.7 test, checking distance < 0.05 left the guard stuck on 57 of 200 patrol legs at 4 m/s and 30 fps. move_toward() never goes past the point, so every leg ended exactly on it.
The code
Fill points with your patrol positions, then every physics frame set the guard's position to step(position, delta). For a CharacterBody3D, turn the result into a velocity, (result - global_position) / delta, and call move_and_slide().
extends Node
## Walks a guard along patrol points without overshooting or jittering at a point.
## Fill points, then every physics frame: guard.global_position = patrol.step(guard.global_position, delta)
@export var speed := 4.0 # meters per second
@export var wait_time := 1.0 # seconds to stand at each point
var points: Array[Vector3] = []
var index := 0
var _wait := 0.0
func step(pos: Vector3, delta: float) -> Vector3:
if points.is_empty():
return pos
if _wait > 0.0:
_wait -= delta
return pos
var target := points[index]
var next := pos.move_toward(target, speed * delta) # stops exactly on the point, never past it
if next.is_equal_approx(target):
index = (index + 1) % points.size()
_wait = wait_time
return next
What we measured
200 patrol legs of random length from 2 to 10 m, the same set for every test, at 4 and 8 m/s and at 30, 60 and 144 fps. A guard counted as stuck if it still had not arrived 5 seconds after it should have. Two runs with the same numbers.
- distance < 0.05: at 4 m/s, stuck on 57 legs at 30 fps and none at 60 or 144. At 8 m/s, stuck on 127 at 30 fps, 57 at 60 and none at 144.
- distance < 0.5: always arrived, but stopped 0.36 to 0.49 m before the point on average, so it never stood where you placed it.
move_toward(): arrived on all 200 legs at every speed and frame rate, 0.000 m from the point.
Why it happens
At 4 m/s and 30 fps one step is 0.133 m. If the guard is 0.07 m from the point, the next step ends 0.063 m past it, and the one after that comes back to 0.07 m short, and so on. Both are more than 0.05, so the check never passes. It can only happen when a step is more than twice the arrival distance, which is why it shows up with fast guards or a low frame rate, often only on someone else's computer. move_toward() moves by the smaller of the step and the distance left.
What to change
speed: walking speed. Raise it for an alerted guard, the script still lands on each point.wait_time: how long the guard stands and looks around at each point.
What the guard sees: vision cone with line of sight.
Ready-made stealth effects: Stealth & Detection VFX for Godot 4 has 32 presets in Stylized and Retro PSX looks, including vision cones in calm, suspicious and alert colors, a detection meter, a Spotted alert, noise rings and a security camera sweep.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Stealth & Detection VFX for Godot 4 has 32 ready-made stealth effects in Stylized and Retro PSX looks, including vision cones in calm, suspicious and alert colors, a detection meter, a Spotted alert and noise rings.
More how-tos for Stealth & Detection VFX for Godot 4: Enemy vision cone with a "!" when it spots you
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene