Search Unity

Instantiating GameObjects at random screen positions

Discussion in 'Scripting' started by brandincanfield, Jul 20, 2016.

Thread Status:
Not open for further replies.
  1. brandincanfield

    brandincanfield

    Joined:
    Feb 8, 2016
    Posts:
    2
    I am trying to randomly spawn this gameobject within the bounds of the screen. My objects spawn randomly, but only within the top left quadrant of the screen. They dont spawn within the whole screen, just the top left half. Any help would be appreciated. Here is what my code looks like:

    public GameObject banana;

    void Start()
    {
    for (int i = 0; i < 10; i++)
    {
    float spawnY = UnityEngine.Random.Range(0, Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
    float spawnX = UnityEngine.Random.Range(0, Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.width)).x);

    Vector2 spawnPosition = new Vector2(spawnX, spawnY);
    Instantiate(banana, spawnPosition, Quaternion.identity);
    }
    }
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Code (CSharp):
    1. void Start()
    2.     {
    3.         for (int i = 0; i < 10; i++)
    4.         {
    5.             float spawnY = Random.Range
    6.                 (Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y, Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
    7.             float spawnX = Random.Range
    8.                 (Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);
    9.  
    10.             Vector2 spawnPosition = new Vector2(spawnX, spawnY);
    11.             Instantiate(banana, spawnPosition, Quaternion.identity);
    12.         }
    13.     }
    try this. I think you should not take just zero. Convert zero to worldPoint.
     
  3. brandincanfield

    brandincanfield

    Joined:
    Feb 8, 2016
    Posts:
    2

    Thank you very much. This was the solution.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Another way to get a random screen position is like this:

    Code (CSharp):
    1. Vector2 randomPositionOnScreen = Camera.main.ViewportToWorldPoint(new Vector2(Random.value, Random.value));
     
    AlexisGervacio and zivmahluf like this.
  5. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    You could also avoid all of this together and pretty much just make an empty game object inside the inspector called Spawn Points or something of the like.

    Then simply add however many empty transforms (at the desired locations on the screen) as you want to the SpawnPoints object in the editor.

    Then create an empty transform array, and populate it with all of these transforms.

    In your Start function, just assign a random integer using Random.Range passing it 0, transformsArray.Length then simply say, banana.transform.position = transformArray[randomInteger].transform.position

    No need for a for loop! :D
     
    khrysller and DomenicoConte like this.
  6. RizzyJ

    RizzyJ

    Joined:
    Oct 25, 2020
    Posts:
    1
    In the background of Camera.Main it executes FindGameObjectsWithTag("MainCamera"). Searching the entire scene for objects using any type of FindGameObjects method is very expensive and should be avoided at all costs. To improve this simply cache the main camera somewhere and use that reference instead of Camera.Main.
     
  7. YoghurtDrop

    YoghurtDrop

    Joined:
    Apr 30, 2021
    Posts:
    1
    RoobrArcade likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Please don't necro 7 year old threads.
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.