Random Mob Spawning, need help and ideas

I’m making a game in which you have to survive different types of creatures that will try to kill you, I’ve set up a health bar, Enemy AI and scene, but I want to make a script that will spawn animal prefabs that wander around the scene at least 16 metres away from the player instead of using waypoints.

A bit like Minecraft. The animal mobs spawn at least 16 metres away and they don’t follow waypoint paths. they just wander to one spot, and then to another.

How do I do this? I have been looking for sites that can help me, but i can’t find any. Sorry, I’m very new to scripting. I just started using unity last week. :expressionless:

If I was doing something like this, I’d break it down into 2 things: Creature Spawning and Creature Behaviour. Breaking a problem down is always a good way to start looking for a solution. I won’t go into any code, but i’ll try to give you a few ideas how to go about doing each one.

Creature Spawning
From your description, the Creature Spawning you want is quite simple. You basically want a script running in your scene that manages when and where to spawn creatures in its update function.

When to spawn them might be complicated depending exatly on what you want to do. The update funtion of your script will be running every update loop of your game, so first you probably want to create a variable to store the last time you spawned creatures, and check an adequte ammount of time has passed before you do it again. This will help avoid you spawning too many creatures. You may also want to consider population control - having a max number of creatures and not spawning more than these, removing creatures that have become too distant from the player, and even having a minimum number of creatures then forcing your next spawn early to make sure the player doesn’t run out of things to kill.

Where to spawn them is quite simple, as you stated you want them X many meteres from the player, so you can use the current player postion plus whatever offset you require to give the creatures a start point. You’d also probably want to add a random value as well to give them some more variation in where they appear.

Creature Behaviour
You said you’ve done some Enemy AI already, so i’ll just cover the basics of making something appear to wander around - it’s actually quite simple to do. Each creature would be responsible for its own location, so simply use the creatures current location plus some random values to give a destination point. When the creature reaches the destination point, you simply calculate an new point. If you use a range that includes negative to posative numbers for you random values it will make sure the creature isn’t always going the same direction. You could make this more complex by ‘leashing’ the creatures to their start point to make sure they don’t wander off too far.

What I’ve suggested are by no means definative answers on how you should go about things, just a few basic (very basic!) suggestions on what direction you might want to take.

Good luck, and welcome to the forums :slight_smile:

Thank you soo much for the ideas! I really apreciate this!

You may want to read up on the theory about mob spawning. Look up the Left 4 Dead “Director”.