← Pixel Dreamer Studio

Spawn enemies where the camera cannot see them

Try random points around the player, keep one only when Camera3D.is_position_in_frustum() is false for the feet and the head, and move further out if none is hidden.

Pick a random point around the player and keep it only when camera.is_position_in_frustum() is false for the enemy's feet and for its head. If every try at that distance is on screen, move further out. In our Godot 4.7 test, with a third-person camera, 53% of random points 15 m from the player were on screen. The script put 0 of 2,000 on screen.

The code

Add it as a child of your spawner and call spawn_point(camera, player.global_position) before you add each enemy. Set height to how tall your enemies are.

extends Node
## Pick a spawn point on the ground around the player that the camera cannot see.
## Checks the feet and the head. If every spot at this distance is on screen, it moves further out.

@export var radius := 15.0  # meters from the player
@export var height := 2.0  # how tall your enemies are
@export var tries := 24


func spawn_point(camera: Camera3D, player_pos: Vector3) -> Vector3:
	var r := radius
	var p := player_pos
	for step in 6:
		for i in tries:
			var a := randf() * TAU
			p = player_pos + Vector3(cos(a), 0.0, sin(a)) * r
			if not camera.is_position_in_frustum(p) and not camera.is_position_in_frustum(p + Vector3.UP * height):
				return p
		r *= 1.25  # nothing hidden at this distance: the camera sees all around it, so go further out
	push_warning("No hidden spawn point found, spawning on screen")
	return p

What we measured

The camera sat 6 m up and 10 m behind the player, looking at a point 1 m above the player, in a 960 by 540 window with the default 75 degree field of view. 2,000 random points on a circle around the player, enemies 2 m tall, two runs with the same numbers.

Why it happens

A circle around the player is not the shape of what the camera sees. In front of a third-person camera the view is wide and reaches far, so a big part of any circle falls inside it. Close to the player, the view covers the whole circle, and a point just below the bottom edge of the screen still shows the top of a tall enemy. is_position_in_frustum() asks the camera itself, so it uses the camera's current position, angle and field of view.

What to change

Walls are not checked: a point behind a wall can still be in the frustum. If your levels have walls, add a raycast from the camera as well.

Random points inside a circle instead of on it: random point in a circle clumps in the middle.

For the enemies the player does see arrive: Spawn, Death & Respawn VFX for Godot 4 has Enemy Spawn in Fire, Frost and Shadow, Summon Circle and Grave Rise.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Spawn, Death & Respawn VFX for Godot 4 has Enemy Spawn in Fire, Frost and Shadow, Summon Circle, Grave Rise, Player Respawn and Boss Death.

More how-tos for Spawn, Death & Respawn VFX for Godot 4: Dissolve shader for 3D models · Death particles disappear when the enemy is freed

More how-tos: Game systems