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.
- 15 m from the player: 53.0% of the points were on screen. At 40 m: 37.0%.
- 8 m: the feet were on screen for 85.2% of the points, and for the other 14.8% the head was. Every point could be seen.
- 6 m: 100% on screen.
- The script: 0 of 2,000 on screen at 6, 8, 15 and 40 m. At 15 and 40 m every point stayed at that distance. At 6 and 8 m it moved out, at most to 11.72 m and 12.5 m.
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
radius: how far from the player to look first, in meters.height: the height of your tallest enemy.tries: random points per distance before it moves out by a quarter.
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
- 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
- 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
- A vision cone that does not flicker at the edge
- 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