Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

random platforms

Discussion in 'Scripting' started by castoldigabriele, Feb 17, 2018.

  1. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    Hi,
    how can I make objects randomly? These objects will have to move to the left and over time they should grow more and more.
    you could send me the script to do this or recommend a guide or tutorial to do that?

    Thank you!!:)
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Usually you use Random.Range to get random numbers:
    https://docs.unity3d.com/ScriptReference/Random.Range.html
    You instantiate prefabs
    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    You make them grow by changing the scale over time
    https://docs.unity3d.com/ScriptReference/Transform-localScale.html

    I don't know of any tutorials that will help you with the scripting of it other than the general ones in the learn section. It sounds like you may need them before attempting this project.
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you want random objects, so a List of objects to randomly choose from.
    Do you want them to spawn in the same place every time? or spawn in a random location? or spawn every X distance apart from the last spawned object?
    Change the scale over time, do you want a max size?
     
  4. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    I would like them to spawn rectangle-shaped objects in the same place every time.
    the length of these objects should change randomly, with a maximum and a minimum width.
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    create an empty GameObject in the scene. add this script to it.
    Put in some values for the min and max and speeds.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnPlatform : MonoBehaviour {
    6.  
    7.     public Vector3 SpawnPosition;
    8.     public float MaxWidth;
    9.     public float MinWidth;
    10.     public float MaxLenth;
    11.     public float MinLenth;
    12.     public float MoveSpeed;
    13.     public float ScaleSpeed;
    14.     private GameObject cube;
    15.  
    16.  
    17.     void Start () {
    18.         cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    19.         cube.transform.position = SpawnPosition;
    20.         cube.transform.localScale = RandomLenth();
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         cube.transform.position += -Vector3.right * Time.deltaTime * MoveSpeed;
    26.  
    27.         cube.transform.localScale += new Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime) * ScaleSpeed;
    28.  
    29.     }
    30.  
    31.     Vector3 RandomLenth()
    32.     {
    33.         return new Vector3(Random.Range(MinLenth, MaxLenth),1,1);
    34.     }
    35. }
     
  6. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    Thank you very much !!!!
    How do I change the spawn position?
    And how can I make a cube spawn every 3 seconds?
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    You change the spawn position in the inspector. click on the gameobject that has the script. then look for the word "Spawn Position". it will have 0, 0, 0. change this to what ever you want. like 100, 0, 5.
    You could also change this from a Vector3 to a gameobject if you want to use an object in the scene as a spawn point.

    Every 3 seconds can be done via a Coroutine or by keeping track of time.
    Home work for you.
    Research a Coroutine and WaitForSeconds
     
  8. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    ok thank you,
    how this script can works with 2d objects???
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    2d? sprites? yes it can.
     
  10. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    i try to do this script but there is an error, how can i fix it??
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnPlatform : MonoBehaviour {
    6.  
    7.     public Vector3 SpawnPosition;
    8.     public float MaxWidth;
    9.     public float MinWidth;
    10.     public float MaxLenth;
    11.     public float MinLenth;
    12.     public float MoveSpeed;
    13.     public float ScaleSpeed;
    14.     private GameObject cube;
    15.  
    16.  
    17.     IEnumerator Start ()
    18.     {
    19.         yield return StartCoroutine (Spawn ());
    20.     }
    21.  
    22.  
    23.     void Start () {
    24.         cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    25.         cube.transform.position = SpawnPosition;
    26.         cube.transform.localScale = RandomLenth();
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         cube.transform.position += -Vector3.right * Time.deltaTime * MoveSpeed;
    32.  
    33.         cube.transform.localScale += new Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime) * ScaleSpeed;
    34.  
    35.     }
    36.  
    37.     Vector3 RandomLenth()
    38.     {
    39.         return new Vector3(Random.Range(MinLenth, MaxLenth),1,1);
    40.     }
    41.  
    42.     IEnumerator Spawn()
    43.     {
    44.         while (true) {
    45.             yield return new WaitForSeconds (Random.Range (1, 5));
    46.         }
    47.     }
    48.  
    49. }
    50.  
     
  11. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    can you post the error message.
     
  12. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I see the error, you have 2 Start() functions. You can't do that.
     
  13. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is my updated script. This looks cool when I run it. It's like a building that keeps growing.
    I'm sure the next thing you'll ask for is how to change the spawn location so they don't spawn on top of each other.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnPlatform : MonoBehaviour
    6. {
    7.  
    8.     public Vector3 SpawnPosition;
    9.     public float MaxWidth;
    10.     public float MinWidth;
    11.     public float MaxLenth;
    12.     public float MinLenth;
    13.     public float MoveSpeed;
    14.     public float ScaleSpeed;
    15.     private List<GameObject> AllCubes;
    16.  
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         AllCubes = new List<GameObject>();
    22.         StartCoroutine(Spawn());
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         for (int i = 0; i < AllCubes.Count; i++)
    28.         {
    29.             AllCubes[i].transform.position += -Vector3.right * Time.deltaTime * MoveSpeed;
    30.  
    31.             AllCubes[i].transform.localScale += new Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime) * ScaleSpeed;
    32.         }
    33.     }
    34.  
    35.     void CreateNewPlatform()
    36.     {
    37.         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    38.         cube.transform.position = SpawnPosition;
    39.         cube.transform.localScale = RandomLenth();
    40.         AllCubes.Add(cube);
    41.     }
    42.  
    43.     Vector3 RandomLenth()
    44.     {
    45.         return new Vector3(Random.Range(MinLenth, MaxLenth), 1, 1);
    46.     }
    47.  
    48.     IEnumerator Spawn()
    49.     {
    50.         while (true)
    51.         {
    52.             yield return new WaitForSeconds(Random.Range(1, 5));
    53.             //after the pause do something here...
    54.             CreateNewPlatform();
    55.         }
    56.     }
    57.  
    58. }
    59.  
     
  14. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    thank you so much
    and if I wanted the spawn time to increase spawn after spawn.. what should i do???
     
  15. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think this is what you're after.

    Code (CSharp):
    1. IEnumerator Spawn()
    2.     {
    3.         float waitTime = Random.Range(1, 5); //set the random value once.
    4.         while (true)
    5.         {
    6.             yield return new WaitForSeconds(waitTime);
    7.             //after the pause do something here...
    8.             CreateNewPlatform();
    9.             //decrease the waittime, to speedup how aften it spawns
    10.             waitTime -= 0.01f; //what ever value works for you.
    11.         }
    12.     }
     
  16. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    thank you so much
    and if I wanted the velocity of cube to increase spawn after spawn.. what should i do???
     
  17. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you don't have a Rigidbody attached to the cube, So you can't increase velocity.

    On line 12, i have MoveSpeed option for you. so change that value and the cube will speed up or slow down.
    So the question is, are you going to change the speed every frame? does each cube going to have it's own speed but never changes over time?

    edit:
    reread the question, change speed after each spawn, that can be done. one sec.
     
  18. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Code (CSharp):
    1. void CreateNewPlatform()
    2.     {
    3.         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    4.         cube.transform.position = SpawnPosition;
    5.         cube.transform.localScale = RandomLenth();
    6.         AllCubes.Add(cube);
    7.         //change move speed after each spawn
    8.         MoveSpeed += 0.01f;
    9.     }
     
  19. castoldigabriele

    castoldigabriele

    Joined:
    Feb 10, 2018
    Posts:
    49
    Thank you,
    i would like that the speed double when i press one button
    i add this on my screen... but it don't works... how can i fix this script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class spawnplatBC : MonoBehaviour {
    6.  
    7.     public Vector3 SpawnPosition;
    8.     public float MaxWidth;
    9.     public float MinWidth;
    10.     public float MaxLenth;
    11.     public float MinLenth;
    12.     public float MoveSpeed;
    13.     public float ScaleSpeed;
    14.     private List<GameObject> AllCubes;
    15.     private float multiplier = 1f;
    16.  
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         AllCubes = new List<GameObject>();
    22.         StartCoroutine(Spawn());
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         for (int i = 0; i < AllCubes.Count; i++)
    28.         {
    29.             AllCubes[i].transform.position += -Vector3.right * Time.deltaTime * MoveSpeed;
    30.  
    31.             AllCubes[i].transform.localScale += new Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime) * ScaleSpeed;
    32.         }
    33.         if (Input.GetMouseButton(0))
    34.         {
    35.             multiplier = 2f;
    36.         }
    37.         else
    38.         {
    39.             multiplier = 1;
    40.         }
    41.     }
    42.  
    43.     void CreateNewPlatform()
    44.     {
    45.         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    46.         cube.transform.position = SpawnPosition;
    47.         cube.transform.localScale = RandomLenth();
    48.         AllCubes.Add(cube);
    49.         MoveSpeed += 0.01f;
    50.     }
    51.  
    52.     Vector3 RandomLenth()
    53.     {
    54.         return new Vector3(Random.Range(MinLenth, MaxLenth), 1, 1);
    55.     }
    56.  
    57.     IEnumerator Spawn()
    58.     {
    59.         while (true)
    60.         {
    61.             yield return new WaitForSeconds(Random.Range(2, 3));
    62.             CreateNewPlatform();
    63.         }
    64.     }
    65.  
    66. }
    67.  
    68.  
    69.  
     
  20. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    what do you want to multiply? you're changing multiplier from 1 to 2, but nothing uses it.

    here is the fix to the mouse detection.
    Code (CSharp):
    1.         if (Input.GetMouseButtonDown(0)) //you needed to check for the down condition
    2.         {
    3.             multiplier = 2f;
    4.         }
    5.         else
    6.         {
    7.             multiplier = 1f; //for got the f
    8.         }
     
  21. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    that might not be what your after. mouse down would only change it to 2 for frame and then change it back to 1. So it must be that fact that your created a float multiplier, but then you don't use it any where else in the script.