← Pixel Dreamer Studio

A vision cone that does not flicker at the edge

Spot the player inside the cone, but only lose them a few degrees and metres outside it.

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.

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

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