Make a box x% larger than screen size?

I have a box that I require to be about 10% larger than the screen (assuming we are looking directly at the box.) Any way to do this, or do I have to set an option for the box for every resolution?

look in to these commands .. http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

1 Answer

1

Unless you are using an orthographic camera, you have to figure out the distance to the face of the box first. With a perspective camera, the amount the box fills the screen will vary based on the distance from the camera. So if you are scaling it uniformly (i.e. the Z size as well), you will need to reposition the box to keep the face at the same distance. To calculate the new size, I’d use Viewport coordinates. They go from (0,0) in the lower left, to (1,1) in the upper right. So if you wanted it 20% oversized, you could use (-0.1, -0.1) and (1.1, 1.1) for inputs. A bit of untested code:

var pos1 = Camera.main.ViewportToWorldPoint(Vector3(-0.1, -0.1, dist_to_box));
var pos2 = Camera.main.ViewportToWorldPoint(Vector3( 1.1,  1.1, dist_to_box));

var width  = Mathf.Abs(pos2.x - pos1.x);
var height = Mathf.Abs(pos2.y - pos1.y);