Enemy Spawner


I don't want to have all of the enemies in the game just hanging out in the world. So, I made a little tool that spawns encounters for the player as they enter a collider. These encounter spawners have a collection of spawn points, a prefab to spawn, a number of prefabs, optional victory conditions, and what the victory objects are. The code on the back end checks if a player has entered into the collider, and when one does it instantiates prefabs at one of the spawn points chosen randomly until it has created an amount equal to the amount allowed. It places all of the prefabs inside of a randomly named game object. Then it checks to see how many objects are inside of that randomly named game object. Once the count is zero, it creates victory condition objects in each of the transforms for them. It destroys itself as well as it completes doing it's job. There's a lot of ways to make this better, but I thought I'd share it here. 


This image shows what one of these spawners prefabs look like. It's really just a bunch of empty game objects with the script applied to the Encounter Spawner. Victory conditions is collapsed here, but it is just a collection of empty game objects as well. 


All of these are assigned in the inspector. 


Below is the code for it. 

public class EncounterSpawnerController : MonoBehaviour
{
    protected bool Started = false;
    public Transform SpawnPoint1;
    public Transform SpawnPoint2;
    public Transform SpawnPoint3;
    public Transform SpawnPoint4;
    public GameObject PrefabToSpawn;
    private GameObject holder;
    public bool HasVictoryCondition;
    public List<Transform> VictoryTransforms;
    public GameObject VictoryObjects;
    //public Transform TriggerLocation;
    public Collider2D Trigger;
    //protected LayerMask PlayerLayer;
    protected GameObject player;
    public int NumberToSpawn = 1;

    private void Start()
    {
        player = GameObject.FindWithTag("Player");
    }

    private void Update()
    {
        if(HasVictoryCondition == true && InstantiatedMobsAreDead() && Started == true)
        {
            foreach (Transform vct in VictoryTransforms)
            {
                Instantiate(VictoryObjects, vct.position, Quaternion.identity);
            }
        }
    }

    protected bool InstantiatedMobsAreDead()
    {
        if (Started == true)
        {
            if (holder.transform.childCount <= 0)
            {
                return true;
            }
            return false;
        }
        return false;
    }

    void OnTriggerEnter2D(Collider2D obj)
    {
        if (obj.gameObject.tag == "Player")
        {
            holder = Instantiate( new GameObject(Path.GetRandomFileName()), GetRandomSpawnPoint(), Quaternion.identity);
            holder.transform.parent = transform;
            for (int i = 0; i < NumberToSpawn; i++)
            {
                var toadd = Instantiate(PrefabToSpawn, GetRandomSpawnPoint(), Quaternion.identity) as GameObject;
                toadd.transform.parent = holder.transform;
            }
            Started = true;
            Destroy(Trigger);
        }
    }

    public Vector3 GetRandomSpawnPoint()
    {
        var test = Random.Range(1, 4);
        switch (test)
        {
            case 1:
                return SpawnPoint1.position;
            case 2:
                return SpawnPoint2.position;
            case 3:
                return SpawnPoint3.position;
            case 4:
                return SpawnPoint4.position;
            default:
                return SpawnPoint1.position;
        }
    }
}

Get Nib

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.