Take the angle between the enemy's forward direction and the direction to the player, then cast one ray so walls block the view. Forward in Godot is -Z, so use -global_basis.z. In our Godot 4.7 test, using +Z got it backwards: the enemy missed a player 4 m in front of it and saw one 4 m behind it. With -Z it saw the player in front and 20 degrees to the side, but not at 40 degrees, not behind, not 7 m away and not through a wall.

The code
Add this node as a child of the enemy, facing the same way as the model, and drag the player into target. The ray needs something to land on, so the player should have a collision shape. Connect spotted and lost to start and stop the chase.
extends Node3D
## Enemy vision: a cone drawn on the floor, a check that the target is inside it with no wall
## in the way, and a "!" over the enemy's head when it spots the target.
## Add this as a child of the enemy, facing the same way, and drag the player into "target".
signal spotted
signal lost
@export var target: Node3D
@export var view_range := 6.0 ## metres
@export_range(10.0, 180.0) var view_angle := 60.0 ## degrees, from one edge of the cone to the other
@export var eye_height := 1.5 ## where the enemy looks from
@export var target_height := 1.0 ## where on the target it looks at
@export var calm_color := Color(1.0, 0.85, 0.2, 0.3)
@export var alert_color := Color(1.0, 0.2, 0.15, 0.4)
var sees := false
var _mat: StandardMaterial3D
var _mark: Label3D
func _ready() -> void:
var cone := MeshInstance3D.new()
cone.mesh = _cone_mesh()
_mat = StandardMaterial3D.new()
_mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
_mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
_mat.albedo_color = calm_color
_mat.cull_mode = BaseMaterial3D.CULL_DISABLED
cone.material_override = _mat
cone.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
cone.position.y = 0.02 # just above the floor, or the floor hides it
add_child(cone)
_mark = Label3D.new()
_mark.text = "!"
_mark.font_size = 128
_mark.outline_size = 24
_mark.modulate = Color(1.0, 0.25, 0.15)
_mark.billboard = BaseMaterial3D.BILLBOARD_ENABLED
_mark.no_depth_test = true # stays visible when a wall is between the camera and the enemy
_mark.position.y = eye_height + 0.7
_mark.visible = false
add_child(_mark)
func can_see() -> bool:
if not target:
return false
var flat := target.global_position - global_position
flat.y = 0.0
if flat.length() > view_range:
return false
var forward := -global_basis.z # forward in Godot is -Z
forward.y = 0.0
if rad_to_deg(forward.angle_to(flat)) > view_angle * 0.5:
return false
var eye := global_position + Vector3.UP * eye_height
var q := PhysicsRayQueryParameters3D.create(eye, target.global_position + Vector3.UP * target_height)
var body := get_parent() as CollisionObject3D
if body:
q.exclude = [body.get_rid()] # do not let the enemy block its own view
var hit := get_world_3d().direct_space_state.intersect_ray(q)
return hit.is_empty() or hit.collider == target
func _physics_process(_delta: float) -> void:
var now := can_see()
if now == sees:
return
sees = now
_mat.albedo_color = alert_color if sees else calm_color
_mark.visible = sees
if sees:
_mark.scale = Vector3.ONE * 0.2
create_tween().tween_property(_mark, "scale", Vector3.ONE, 0.25).set_trans(Tween.TRANS_BACK).set_ease(Tween.EASE_OUT)
spotted.emit()
else:
lost.emit()
func _cone_mesh() -> ArrayMesh:
var st := SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
var steps := 24
var half := deg_to_rad(view_angle * 0.5)
for i in steps:
var a0 := -half + 2.0 * half * i / steps
var a1 := -half + 2.0 * half * (i + 1) / steps
st.add_vertex(Vector3.ZERO)
st.add_vertex(Vector3(sin(a1), 0.0, -cos(a1)) * view_range)
st.add_vertex(Vector3(sin(a0), 0.0, -cos(a0)) * view_range)
return st.commit()
What we measured
The script as written: 6 m range, a 60 degree cone (30 degrees each side). The player was a sphere collider 1 m above the floor. We read can_see() after two physics frames.
- 4 m in front: seen. 4 m behind: not seen. 20 degrees to the side: seen. 40 degrees: not seen. 7 m in front: not seen.
- The same check with
global_basis.z(+Z) as forward: the player 4 m in front was not seen, the one 4 m behind was. - A wall 2 m in front of the enemy: not seen. Wall removed: seen.
- Enemy turned 90 degrees: the player at its new front was seen, the player at its old front was not. The check turns with the enemy.
- The "!" with a big wall between the camera and the enemy: 10 of the red pixels we checked (every second pixel) with
no_depth_teston, the same as with no wall. With it off: 0. - The cone turned red when the player was seen, and the "!" went away again when the player walked out behind the enemy.
Two runs gave the same results.
Why -Z is forward
Cameras, lights and look_at() in Godot all point down -Z, so a model that faces forward faces -Z too. global_basis.z is the enemy's back. Leaving out the height (flat.y = 0.0) keeps the angle on the floor, so the angle check ignores how much higher or lower the player is. Walls and cover are left to the ray.
What to change
view_rangeandview_angle: the cone mesh is built from them in_ready(), so set them before the node enters the tree.eye_heightandtarget_height: where the ray starts and ends. With a lower target height, low cover between the two blocks the ray.- The cone sits 0.02 m above the floor. At exactly floor height the floor can hide it, the same as ripple rings on water.
- Turn
no_depth_testoff if the "!" should hide behind walls like everything else. - The alert flickering when the player stands at the edge of the cone: lose sight a little later than you spot.
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, noise rings and a security camera sweep.
More how-tos for Stealth & Detection VFX for Godot 4: Walk a patrol guard to each point without overshooting · A vision cone that does not flicker at the edge
More how-tos: Auras, rings, beams and weather
- Light shaft cards without the bright edge-on line
- Stretch a bolt sprite from the caster to the target
- Space lights, posts or coins evenly along a Path3D
- Stop a big spinning texture from looking grainy when it is drawn small
- God rays without volumetric fog
- Dark horror fog that works in Compatibility
- Magic aura particles that circle a character
- Charge-up particles that fly in to the middle
- Particle ring standing up instead of flat
- 2D particles from a sprite's shape
- A glowing circle on the ground
- A loot beam that never goes flat
- Chain lightning between enemies
- A tower's attack range ring
- Rain that follows the camera
- Rain falling through the roof
- Snow particles not falling
- orbit_velocity not working in 3D: use tangential_accel
- Rain falls through the player
- Snow that stays on roofs
- Emit particles along a Path3D
- Ambient particles in many places from one emitter
- Keep a buff aura on until the last buff really runs out
- Spin a magic circle forever at the same speed
- Space flying swords, shields or orbs evenly around a character for any count
- Text around a circle with even gaps