Search Unity

How do I make an open space enemy spawner?

Discussion in 'Scripting' started by tomicz, Dec 27, 2020.

  1. tomicz

    tomicz

    Joined:
    Mar 16, 2015
    Posts:
    152
    Hello there,

    I am trying to build a spawner for enemies in an open-world game. Actually, it's a space game, so there are not many obstacles except asteroids and similar. What is the best design pattern to achieve this? I want it to be randomly generated.

    This is my original idea and I need to know if this is a good way to do it.

    - Enemies are spawned in a grid
    - I only generate enemy data, like position, type, etc
    - When the spaceship camera is near, the enemy will be activated based on the data.
    - Enemies will be object pooled. I will have a list of enemy types and the maximum number of them of how many players can activate.

    Is this a good idea? And are there some resources online on how to achieve similarly? Hack and slash enemies are similar to ours, so are there some resources?
     
    Last edited: Dec 27, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Ideas can sound reasonable but the devil is always in the implementation details. Keep in mind space gets real big real fast and the user can only see so much around them. Enemies that are "living" far off in the distance add absolutely nothing to a game. It doesn't make sense to fill up a massive spatial volume with enemies if the user can only see the ones a few hundred units around him.
     
  3. tomicz

    tomicz

    Joined:
    Mar 16, 2015
    Posts:
    152
    Enemies will only activate when the camera is near them. I will not fill the level with enemies, I will fill the level with enemy data, like enemy position, type, etc. And then I will get enemy game objects from a pool list and activate them based on the data.
     
  4. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    You could just place an empty transform at the position and then spawn the enemy when the player is near. You can use distance check to determine if the player is close enough. That’s lazy way to do it.

    Another alternative is to create a enemy manager to allow only so many enemies to be spawned at once. If the number of spawned enemies is less than the max amount allowed then you can spawn one at a random vector within a certain distance of the player in a random radius. That will give it a “random endless” feel to it.

    Honestly there are many ways to achieve it.
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    This ^^^ it is FAR more important to get something (ANYTHING) going and see how it feels. Every different way you do it will result in a subtly different experience in the game, so you need to iterate and try stuff, change it, try other stuff, etc.
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    My feeling is to focus on where enemies should appear, and how often. A 12 parsec run, in unpatrolled space, when you met 0 pirate last time might have 1-3 pirate encounters. They'd be lurking near asteroids or space junk, or in the middle of the run, far from any patrols, or they might tend to be at the point nearest the pirate planet. Pirates may also not like to be near each other. Taking a longer path may avoid some of them. Certain types of pirate may prefer certain areas (open space pirate are probably faster). That may give a nicer experience than randomly placing them.
     
    Kurt-Dekker likes this.