Using the same angle and range to spot the player and to lose them makes the alert flicker whenever the player stands near the edge, because every small step crosses the line again. In our Godot 4.7 test a player strafing along the 45° edge of a guard's cone, 8 m away, switched the alert 12 times in 5 seconds, and one standing at the 12 m range edge switched it 9 times. Spotting at 45° and 12 m but losing sight only past 50° and 13.5 m switched it once and 0 times.
The code
Save it as sight_margin.gd, keep one per guard with var sight := preload("res://sight_margin.gd").new(), and every frame call sight.update(eyes.global_transform, player.global_position), where eyes is a node on the guard's head facing -Z. Show the alert mark or fill the detection meter while it returns true. It only checks the cone: once it says the player is inside, cast a ray to the player too, so walls still block the view, as in a vision cone with walls.
extends RefCounted
## A vision cone that spots the player inside it but only loses them a bit
## outside it, so the alert does not flicker on and off at the edge.
## var sight := preload("res://sight_margin.gd").new()
## every frame: if sight.update(eyes.global_transform, player.global_position): ...
## Check walls with a raycast as well, once update() says the player is in the cone.
var half_angle := deg_to_rad(45.0) # spotted inside this angle from the eyes' -Z
var view_range := 12.0 # and this close
var lose_angle := deg_to_rad(5.0) # lost only this far past the angle
var lose_range := 1.5 # or this far past the range
var seeing := false
func update(eyes: Transform3D, target: Vector3) -> bool:
var to := target - eyes.origin
var dist := to.length()
if dist < 0.001:
seeing = true
return seeing
var angle := (-eyes.basis.z).angle_to(to)
if seeing:
seeing = angle <= half_angle + lose_angle and dist <= view_range + lose_range
else:
seeing = angle <= half_angle and dist <= view_range
return seeing
What we measured
5 seconds at 60 fps (300 frames), a guard at the origin with a cone of 45° each side and 12 m, margins of 5° and 1.5 m for the script. Two runs gave the same numbers.
- Strafing along the side edge 8 m away, 45° ± 1.5° with a small step bob: the plain check switched the alert 12 times and saw the player in 137 of 300 frames. The script switched it once, when it first spotted the player, and kept them for 275 frames.
- Standing at the far edge, 12 m ± 0.3 m and 10° off center: the plain check switched 9 times and saw the player in 135 of 300 frames. The script never switched off: 300 of 300.
- Walking straight away at 2.8 m/s: the plain check lost the player at 12.02 m, the script at 13.51 m. Stepping out sideways at 8 m: lost at 45.0° and at 50.0°.
Why it happens
A single limit has no memory: a player standing on the line is inside one frame and outside the next, and each change restarts the alert sound, the mark and the meter. Keeping the player with a wider limit than the one used to spot them means they have to clearly leave the cone to be lost, so small movements at the edge do not count. The cost is that the guard keeps seeing the player up to 5° and 1.5 m past the drawn cone, so draw the cone at the spotting size and keep the margins small.
What to change
- A guard that walks its route without stopping to shake at each point: a patrol that reaches its waypoints.
- Walls that should block the view: angle, range and a raycast.
What the guard sees: 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, 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, noise rings and a security camera sweep.
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
- Walk a patrol guard to each point without overshooting
- 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
- 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