- Home /
Random spawning in open environment
My game is a boat game and takes place on open water.
The player must collect objects floating in the water while avoiding enemy craft. What I can't figure out is how to randomly spawn enemies so that they aren't always spawning in the same place or at the same time. If I was to use an array and iterate through potential spawn points I'd need 100s of possible spawn points. How should I do this?
Then I'm faced with the question of how to make enemy ships attack the player. Everything I've read so far about AI mentions waypoints but this is unusable in this situation.
Answer by Larry-Dietz · Jan 11, 2012 at 09:48 PM
For the random spawning part of your question, the easiest thing that comes to mind is to use Random.Range to get a random point within your map to spawn an enemy, then use Instantiate to create an instance of your enemy at that point.
As for making the enemy attack, my first thought is to use Vector3.Distance to determing how close your player is to the enemy, and trigger your attack code from there, if the distance is within the range you want.
Alternatively, create a box collider around the enemy, large enough to encompass the range you want the enemy to detect the player at, Check the Is Trigger box on the collider, and launch your attach code in the trigger event.
Hope this is of some help. -Larry
Thanks! I'm just learning but now I know what to research, helped a lot :)