← Pixel Dreamer Studio

Enemy vision cone with a "!" when it spots you

Angle, range and one raycast, with the cone drawn on the floor.

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.

Seen from above: a dark enemy with a cone on the floor in front of it. Left: a blue player just outside the yellow cone. Right: the player inside, the cone red and a red ! over the enemy
Godot 4.7 with the Compatibility renderer. Left: the player just outside the cone. Right: one step inside.

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.

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

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