Search Unity

Enemies on play

Discussion in 'Getting Started' started by Harper_aimee, Mar 14, 2019.

  1. Harper_aimee

    Harper_aimee

    Joined:
    Aug 25, 2018
    Posts:
    10
    Hey

    I need some advice on how I would go about spawning Eminem’s on play. For example when I press play and the game loads, I want thee to be 100 Eminem’s in play? What is the best way to do this?
     
  2. ClaudiaTheDev

    ClaudiaTheDev

    Joined:
    Jan 28, 2018
    Posts:
    331
    First you should listen to the song "Spawn" by Eminem :cool::


    Little joke - i hope you can forgive me;-)!

    I think the best way would be to create prefabs for the enemies you want to spwan and then create the enemies over enemy = GameObject.Instantiate(prefab).
    Then you can set for example the rotation and position of the enemy via.
    enemy.transform.position =
    For random positions you can use the Random class.
     
    Kiwasi and vakabaka like this.
  3. Harper_aimee

    Harper_aimee

    Joined:
    Aug 25, 2018
    Posts:
    10
    You just made me laugh ! Haha I can forgive you.

    So realistically I could just place them in the scene if I wanted to? But each time I play I would want them to appear in a different place. Hmm ok, where would I add the script to each prefab ? I am pretty new to unity so learning. Thank you for your response.
     
  4. ClaudiaTheDev

    ClaudiaTheDev

    Joined:
    Jan 28, 2018
    Posts:
    331
    You could create a GameObject called for example EnemyManager or EnemySpawner in your scene. Create a script (same name or different) for example EnemySpawner.cs and attach it to the object,
    In that you can spawn the enemies on start.


    Code (CSharp):
    1. public class EnemyManager : MonoBehaviour
    2. {
    3.     public Transform enemyPrefab;
    4.     public int enemyCount = 100;
    5.  
    6.     // Use this for initialization
    7.     void Start()
    8.     {
    9.         for (int i = 0; i < enemyCount; i++)
    10.         {
    11.             SpawnEnemy();
    12.         }
    13.     }
    14.  
    15.     private void SpawnEnemy()
    16.     {
    17.         var enemy = GameObject.Instantiate(enemyPrefab);
    18.         enemy.transform.position = GetRandomEnemyPosition();
    19.     }
    20.  
    21.     private Vector3 GetRandomEnemyPosition()
    22.     {
    23.         //Only example
    24.         var position = Random.insideUnitCircle * 10;
    25.         position.y = 0f;
    26.         return position;
    27.     }
    It is up to you how you determine the positions.
    In the example the enemies spawn in a circle with radius 10.
    Hower they can still overlap each other.

    There are many ways to determine a position. You have to find one which suits your game best.
     
    Harper_aimee likes this.
  5. ClaudiaTheDev

    ClaudiaTheDev

    Joined:
    Jan 28, 2018
    Posts:
    331
    When you want to spawn the enemies on play you could create a public method SpwanEnemies and call this one when the button is pressed instead of doing it in OnStart. Also clearing the remaining enemies before creating new ones.

    Code (CSharp):
    1.   public Enemy enemyPrefab;
    2.         public int enemyCount = 100;
    3.  
    4.         public void SpawnEnemies()
    5.         {
    6.             ClearEnemies();
    7.  
    8.             for (int i = 0; i < enemyCount; i++)
    9.             {
    10.                 //Spwan enemies
    11.             }
    12.  
    13.         }
    14.  
    15.         public void ClearEnemies()
    16.         {
    17.             var enemies = FindObjectsOfType<Enemy>();
    18.             foreach (var enemy in enemies)
    19.             {
    20.                 Destroy(enemy.gameObject);
    21.             }
    22.         }
    When you get performance issues you should inform you about pooling the enemies(dont destroy the old ones but reuse them)
     
    Harper_aimee likes this.
  6. Harper_aimee

    Harper_aimee

    Joined:
    Aug 25, 2018
    Posts:
    10
    Cool thank you so much I will try this! Thanks for the help.