← Pixel Dreamer Studio

Walk a patrol guard to each point without overshooting

Move with move_toward(target, speed * delta), which stops on the point, then go to the next one.

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.

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

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