How can I get the x position for the left(and right) of the screen?

In Unity, I am making a game where spheres spawn at a random position. I want the spheres can spawn a little outside the screen. This is the code where the spheres spawn:

Instantiate (sphere_prefab, new Vector3 (Random.Range (-7, 7), 10, 0), Quaternion.identity);

On a mobile it works great and the cubes can spawn (half)outside the screen, but on my pc, the spheres will not spawn outside the screen cause the screen is bigger so I want to know the left position of the screen so the code can be like this

Instantiate (sphere_prefab, new Vector3 (Random.Range (screenLeft-2, screenRight+3), 10, 0), Quaternion.identity);

Sorry for my bad English.
Thanks in advance!

You can use Screen.width and Screen.height to get the size of the Screen.

But that is in pixel units.

So you would have to convert it.

What you could do is this:

float offset = 20; // Change this value to something you like.

Instantiate (sphere_prefab,
new Vector3(Random.Range (Screen.width / 2.0f - offset, Screen.width / 2.0f + offset),
10, 0), Quaternion.identity);

But the offset value need to change dependent on what aspect ratio you are running.

The aspect ratio is the (Screen.width / Screen.height).

So for example:

void Start()
{
   if((Screen.width / Screen.height) == 1.6f)
      offset = 25.0f; // Some value that works for this aspect
}

I hope this will help you abit. :slight_smile:

I already figured out the answer, Use viewportpoint; 0,0 is left under 1,1 is upper right