Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Random Spawnpoint's

Discussion in 'Scripting' started by TheSaviour01, Apr 6, 2016.

  1. TheSaviour01

    TheSaviour01

    Joined:
    Apr 2, 2016
    Posts:
    30
    Hey i have a question

    im new in the C# coding and im following a tutorial for a shooter game here on unity

    and for the spawning of me enemies i have en pre writen script

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyManager : MonoBehaviour
    4. {
    5.     public PlayerHealth playerHealth;
    6.     public GameObject enemy;
    7.     public float spawnTime = 3f;
    8.     public Transform[] spawnPoints;
    9.  
    10.  
    11.     void Start ()
    12.     {
    13.         InvokeRepeating ("Spawn", spawnTime, spawnTime);
    14.     }
    15.  
    16.  
    17.     void Spawn ()
    18.     {
    19.         if(playerHealth.currentHealth <= 0f)
    20.         {
    21.             return;
    22.         }
    23.  
    24.         int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    25.  
    26.         Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    27.     }
    28. }
    29.  
    this one but how can i change this script for random spawn point now i need to make a point in me scene en assign the enemy but a want it to chose a random location in the map
     
  2. Magison

    Magison

    Joined:
    Mar 1, 2013
    Posts:
    21
    Code (csharp):
    1.  
    2.  
    3. void Start() {
    4.  
    5. // set all spawnpoints to random positions
    6. foreach (Transform sp in spawnPoints)
    7. {
    8.     sp.position = new Vector3( Random.Range( 0f, worldLenght), 0f, Random.Range( 0f, worldWidth));
    9. }
    10.  
    11. InvokeRepeating ("Spawn", spawnTime, spawnTime);
    12. }
    13.  
    14.  
    Add the foreach-loop into your Start() function.
    worldLenght and worldWidth should be float values of the dimensions of your terrain.

    if you use the Unity Terrain as your world, you can get the world size by this script:

    Code (csharp):
    1.  
    2.  
    3. void Start() {
    4.  
    5.    // get the size of the terrain
    6. Terrain t = GameObject.FindObjectOfType(typeof(Terrain)) as Terrain;
    7. float worldLenght = t.terrainData.size.x;
    8. float worldWidth = t.terrainData.size.z;
    9.  
    10.   // set all spawnpoints to random positions
    11. foreach (Transform sp in spawnPoints)
    12. {
    13.     sp.position = new Vector3( Random.Range( 0f, worldLenght), 0f, Random.Range( 0f, worldWidth));
    14. }
    15.  
    16. InvokeRepeating ("Spawn", spawnTime, spawnTime);
    17. }
    18.  
    19.  
     
    Last edited: Apr 6, 2016
  3. TheSaviour01

    TheSaviour01

    Joined:
    Apr 2, 2016
    Posts:
    30
    can i just copy paste that i me script en edit the x and y ?

    Because when i try it gives a error Mathf does not contain RandomRange
     
    Last edited: Apr 6, 2016
  4. Magison

    Magison

    Joined:
    Mar 1, 2013
    Posts:
    21
    Yes. Copy this into your Start() function.
    Leave the rest of your code like it is. It is ok.

    If you use the Unity Terrain, copy/paste the second script.

    If you use a "self made" terrain (for example a big plane) you should use the first code and insert the x and y dimensions for worldLenght and worldWidth.
     
  5. Magison

    Magison

    Joined:
    Mar 1, 2013
    Posts:
    21
    Oh. My mistake.

    It is called Random.Range() and not Mathf.RandomRange.
    I edited the code above to the correct spelling.
    Now you can copy/paste it.
     
  6. TheSaviour01

    TheSaviour01

    Joined:
    Apr 2, 2016
    Posts:
    30

    i dont know if it is a unilty terain i guess because it's a tutorial form unity

    and i will try c:
     
  7. TheSaviour01

    TheSaviour01

    Joined:
    Apr 2, 2016
    Posts:
    30
    when i try the code it only chose random ones but i want to do is to chose a ramdom loctation every spawn