I have an enemy spawner but I can spawn where I want to

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MemokingMH

I have an enemy spawner and it works really good, it picks a random enemy from a list of enemies and spawns it at the position I input. I can randomize the spawning frequency and position but the problem starts here. I want enemies to spawn randomly but not too close to player. When I try telling it to randomize position until it’s far enough from player and it works most of time but it sometimes causes stack overflows.

My only idea is using a randomizer to pick a number from 1 to 4 and spawn an enemy in corresponding side(Left, Right, Up, Down) with enough gap between player and enemy but I don’t want to use a bunch of if else statements(I did it before and code gets too messy and unnecessarily long). There must be an easier way to do things, can someone help with finding a solution maybe link a documentation or a video, or just explain themselves

:bust_in_silhouette: Reply From: toxicvgl

Here is one idea I learned from a GDQuest tutorial (see from 32:20):

The idea is to make a big spawning circle around the player by using a Path2D node (though a code is needed to make a round circle).

Yeah I think it does the job, thank you so much

MemokingMH | 2022-10-19 16:06