Menu

What I learned spawning enemies off-screen in Godot 4
📰

What I learned spawning enemies off-screen in Godot 4

DEV Community: tutorial·saltmire·7 days ago
#FzNUoPZD
#devtotutorial#devto#dev#player#screen
Reading 0:00
15s threshold

Every horde game needs enemies to appear somewhere , and the player should never see the

moment they appear. That sounds like one line of code. It took me five fixes to get a

spawner that doesn't pop enemies in on screen, inside walls, or outside the map. Here is

what each fix taught me.

Lesson 1: a fixed radius only works at one zoom level

The first version everybody writes picks a random angle around the player and pushes the

point out by a fixed distance:

@ export var spawn_radius : = 700.0

func _spawn_point () -> Vector2 : var angle : = randf () * TAU return player . global_position + Vector2 . RIGHT . rotated ( angle ) * spawn_radius

Enter fullscreen mode

Exit fullscreen mode

It's fine as long as nothing changes. Then I zoomed the camera out for a boss and enemies

started popping in on screen. Later I tested on a wider window and it happened again.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.