# Found a way to pick a random point around the viewport boundaries

Hi there,

Been searching quite some time for a proper solution on how to get a random position, around the orthographic boundaries.
Since i wasn’t able to find any suitable solution, i managed to create one myself.

I’m just putting this snippet out there, for anyone sitting in my position and just needs a solution for now.
Im sure this could be prettier in some way but it does the job for now

Have fun.

``````/**
* Get a random position outside our orthographic boundaries
*/
private Vector3 GetRandomPosOffScreen() {

// Booleans for up, down, left and right
bool isXAxis = Random.Range(1f, 10f) < 5f; // True = X axis | False = Y Axis
bool isLeftSide = Random.Range(1f, 10f) < 5f; // True = Left side | False = Right side
bool isUpSide = Random.Range(1f, 10f) < 5f; // True = Up | False = Down

// Getting the actual height and width of the game scene (viewport)
float screenHeight = Camera.main.orthographicSize * 2.0f;
float screenWidth = screenHeight * Camera.main.aspect;

// Some extra space along the boundaries
float offset = 2f;

// Defining the edges of the boundaries with added offset
float minusWidth = -screenWidth / 2f - offset;
float plusWidth = screenWidth / 2f + offset;
float minusHeight = -screenHeight / 2f - offset;
float plusHeight = screenHeight / 2f + offset;

if (isXAxis) {
// Returning a random x axis coordinate
return new(isLeftSide ? minusWidth : plusWidth, Random.Range(minusHeight, plusHeight), 0);
} else {
// Returning a random y axis coordinate
return new(Random.Range(minusWidth, plusWidth), isUpSide ? minusHeight : plusHeight, 0);
}
}
``````
1 Like

Prettier way:

``````Vector3 randomPoint = new(Random.Range(0f, 1f), Random.Range(0f, 1f));
randomPoint.z = 10f; // set this to whatever you want the distance of the point from the camera to be. Default for a 2D game would be 10.
Vector3 worldPoint = Camera.main.ViewportToWorldPoint(randomPoint);
``````

You’re right, i just don’t see this going outside the screen borders which is what i was searching for.

I believe it would work if you provide values less than 0 and greater than 1.
If you’re asking only for points OUTSIDE the screen, you should be able to do something like this:

``````float x = Random.Range(-0.2f, 0.2f);
float y = Random.Range(-0.2f, 0.2f);
if (x >= 0) x += 1;
if (y >= 0) y += 1;
Vector3 randomPoint = new(x, y);
``````

Then continue as above with ViewportToWorldPoint.

2 Likes

@PraetorBlue If i had just found your example sooner, i could’ve saved a few hours
Turns out, this works just fine like my example does. Nice work

``````/**
* Get a random position outside our orthographic boundaries
*/
private Vector3 GetRandomPosOffScreen() {

float x = Random.Range(-0.2f, 0.2f);
float y = Random.Range(-0.2f, 0.2f);
x += Mathf.Sign(x);
y += Mathf.Sign(y);
Vector3 randomPoint = new(x, y);

randomPoint.z = 10f; // set this to whatever you want the distance of the point from the camera to be. Default for a 2D game would be 10.
Vector3 worldPoint = Camera.main.ViewportToWorldPoint(randomPoint);

return worldPoint;
}
``````

See my updated example code, as I believe using Mathf.Sign is a bug (since you’ll get -1.2 instead of -0.2 for example)

2 Likes