Enemy Spawning

I am trying to get an enemy to spawn in 10 different locations using Vector3 or just anchoring the spawn to an invisible object. I need it so that when a collider, attached to the player, collides with the location the enemy will spawn. It will then chase the player for about a minute and will then disappear. There must only be one enemy at a time on the map. My only problem here is I’m not quite certain on how to code this. I’ve gone through the tutorials on the Unity site but none of them specify how to do this.

Well, this playlist isn’t from the Unity Site as far as I know, but he has enemies that spawn, so he could be of use to you.

This question speaks about spawning scripts, but isn’t explicit about what to apply it to. Perhaps you know. If so, please tell me!

Hopefully this helps you as much as it helped me.

Hi!

You could code this by defining each spawn location as an empty game object with a simple triggered collider (capsule collider or anything else which suits your needs).
Every spawn game object will have attached a script which will use the event OnTriggerEnter() to do two things:

  • Look if an Enemy exists
  • If the space is empy, spawn an enemy in the location.

In order to check for enemies the enemy prefab will have to be tagged as “Enemy”.

I attach a pseudo-code example

void OnTriggerEnter(Collider other)
    {
        
        //If somebody who's not the player is colliding, don't spawn
        if(other.tag != "Player")
           return;

        GameController handle2otherEnemies= GameObject.FindWithTag("Enemy");

        if(handle2otherEnemies!=null) //Ok, there is another enemy in game
           return;
        else //There is nobody
          Instantiate(Enemy,transform.position,transform.rotation);

      
    
        
    
    }

Just remember to:

  • create a public GameObject variable named Enemy linked to your Enemy prefab
  • Tag your Enemy prefab as “Enemy” (or whatever you like) and the Player as “Player”

Let me know if you need further assistance!