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 level like Subway Surfers or Minion Rush??

Discussion in 'Scripting' started by engelcj, Jun 10, 2015.

  1. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    Hello everyone!

    I am creating a endless game, I have a few prefaps that are my levels, I now have to generate them randomly in my scene, but I need that they generated adding to the previous object, something such as Subway Surfers or Minion Rush.

    Any idea?
     
  2. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    One Idea would be to make the objects move and not the player. And making them spawn at the same place, and move towards the player. just at increasing intervals or increasing speeds as the player gets further in the game. I don't know how advanced of a infinite runner you are trying to make, but there's one option.
     
  3. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    @Jorjor210 Thank you for your response, but my game have objects more larger than other, then sometimes they will be empty spaces.
     
  4. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    You can use a few C# things to do this.

    Create an Array of GameObjects and place all your prefabs in it. Then use Random.Range(0, myArray.Length);

    Then just instantiate it based on this random number you have generated.
     
    TheDevloper likes this.
  5. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    @GreenBoxInteractive I am using this, but I need that objects one in front of the other, how get it?

    Code (CSharp):
    1. public float distanciaMax;
    2.     public float distanciaMin;
    3.  
    4.     public GameObject[] objetos;
    5.  
    6.     void Start () {
    7.         Generar ();
    8.     }
    9.  
    10.  
    11.     void Generar(){
    12.         Instantiate (objetos [Random.Range (0, objetos.Length)], transform.position, Quaternion.identity);
    13.         Invoke ("Generar", Random.Range (distanciaMax, distanciaMin));
    14.     }
    15. }
    16.  
     
  6. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Code (CSharp):
    1.  void Generar(){
    2.         Instantiate (objetos [Random.Range (0, objetos.Length)], transform.position + offset<-----, Quaternion.identity);
    3.         Invoke ("Generar", Random.Range (distanciaMax, distanciaMin));
    4.     }
     
  7. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    @GreenBoxInteractive It works well if my objects have the same size, but all are of different size.
     
  8. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    Thank you all, I have already fixed it.
     
  9. artwaysurfers

    artwaysurfers

    Joined:
    Jul 1, 2022
    Posts:
    1
    How did you fix it? Can I have the script please?
     
    Last edited: Jul 11, 2022