How to make enemies spawn

Godot Version

Replace this line with your Godot version

Question
how to make enemies randomly spawn along the x axis? and set an amount of how many enemies can spawn?

you could set your newly spawned object’s position.x to a randf_range

var clone = enemy_prefab.instantiate()
clone.position.x = randf_range(-200, 200)

add_child(clone)
2 Likes

does this work for character bodies 2d

As @gertkeno given the codes, it will work for every nodes including character body. That is not matter, it can instantiate any scene and enemy_prefab is the path of the scene or enemy that you want to spawn.

ok ill try that thankyou.

1 Like

Something I like doing for enemy spawn points, is I make a node the enemy spawn. I attach the script to the enemy spawn node and it can control the spawns. I then am able to place the node in the world where I like. And it’s configurable, all the way down to the prefab, inside the node editor. Then I can make make each one unique behavior. A prefab in Godot is actually a scene. A TSCN file.

i knew that, but thanks
the stuff that KingGD and gertkeno helped me with was useful and it worked!