Search Unity

How to create random objects

Discussion in 'Scripting' started by onurduha, Jan 29, 2019.

  1. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    game.png I'm trying to create 12 collectable objects in a random position, but I couldn't do it, so I need your help. it will be something like that in the picture, but each time in a different position. I will be grateful if you could help me
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Random.Range( min, max) will get you a random number from min to max.

    Make one random for X, one random for Y, and put them into your spawned object's transform.position property.
     
    onurduha likes this.
  3. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
    Yes, it worked, but I have two more questions. if they are very close to each other, spawn somewhere else, can I check this. second, if there is an object in the spawned position, spawn to another position, can I check this.
    because i will have enemys to and i dont want my enemy and collectable object to spawn in same position
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  5. onurduha

    onurduha

    Joined:
    Dec 29, 2018
    Posts:
    51
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Yes this is easy: keep a list and check against them. For any number of objects up to a few hundred, just checking all previous will be easily performant enough to do at load time. You just need to decide how close is "too close" and check the distance to all other objects so far placed.

    Another way of obtaining a better distribution in your space is to only randomize one of the axes, and instead iterate the other axis from left to right with some spacing, like so:

    Code (csharp):
    1. for (float x = LeftSide; x <= RightSide; x += HorizontalSpacingAmount)
    2. {
    3.    float y = Random.Range( LowestPoint, HighestPoint);
    4.    Vector3 position = new Vector3( x, y);
    5.  
    6.   // TODO: use position to position your object after you instantiate it
    7. }
    The above code will give a nice even spacing left to right, yet be very vertically randomized. I'll leave you to define the five limit and step variables in the above description, as they are fairly descriptive.

    You can even get trickier and use the ordinal number of your spawn as a "tweener" between the left and right, like so (modifying the above code):

    Code (csharp):
    1. for (int i = 0; i < NumberOfSpawnedObjects; i++)
    2. {
    3. // alpha will go from 0.0f to 1.0f
    4.   float alpha = (float)i / (NumberOfSpawnedObjects - 1);
    5.  
    6. // alpha "drives" the x from left to right
    7.   float x = Mathf.Lerp( LeftSide, RightSide, alpha);
    8.  
    9.   // continue with the Vector3 position = line from the first snippet
    10. }
    That's nice because it frees you from having to create a HorizontalSpacingAmount. If you increase the number of objects, it just packs them tighter, all nice and evenly horizontally.
     
    eses likes this.