Variables to represent the edges of the screen

So what im trying to do is create some self adjusting variables that can be used to represent the edges of the play area. The variables will be used in creating an object on a 2D plane that will be used in a:

Vector3(Random.range(xMin,xMax),Random.range(yMin,yMax),0):

where xMin,xMax,yMin,yMax will be the self adjusting variables to determine what the acceptable area is.

Many thanks in advance and im sorry for any formatting errors i may have made, im still learning.

2 Answers

2

If you want to get the edges of the screen, you can use

Screen.width, Screen.height;

of course, these will only give you positions in screen-space, so you’ll have to do some raycasting to find the actual positions in space (assuming a variable called planeDistance, that determines how far away from the camera the points should be:

topLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, planeDistance));
topRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, planeDistance));
bottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, planeDistance));
bottomRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, planeDistance));

This will give you the four corners of the viewport, in world coordintes.

Thank you very much, i was able to use this to do exactly as i wanted.