- Home /
Stretching a gameobject to fit viewport
Hi! To make my game scale neatly to all screensizes, I need a way to set my Gameobject's scale to fit the viewport. Google has failed me, so I was hoping the lovely folk here might be able to help offer a tip or solution! Thanks in advance!
what script language are you using?
in any case you'll have to set up a call to retrieve the screen resolution, do some math, then scale the object to that size
did u got any correct method so that gameobjects scale and position with different screen sizes?
Answer by robertbu · Feb 01, 2013 at 08:42 AM
With a perspective view, the size of your object will depend on its distance from the camera. So convert Viewport coordinates to World coordinates you have to pick a distance from the camera. For Viewport coordinates the bottom left is 0,0, and the top right is 1,1.
So if you wanted to scale an object that is 10 units in front of the camera. You could do something like (untested):
Vector3 v3ViewPort = new Vector3(0,0,10);
Vector3 v3BottomLeft = Camera.main.ViewportToWorldPoint(v3ViewPort);
v3ViewPort.Set(1,1,10);
Vector3 v3TopRight = Camera.main.ViewportToWorldPoint(v3ViewPort);
Now you have the two corner points in world space you can use to place and size your object.
Another approach is to use the camera's field and the screen aspect ratio. To calculate the height of an object in world units at a specified distance:
Height = 2.0f Mathf.Tan(0.5f Camera.main.fieldOfView) * distance;
You get the width by: Height * Screen.width / Screen.height;
Answer by ataxkt · Feb 04, 2013 at 05:35 PM
Fantastic. Took the first approach, which works perfectly when I change the distance variable to reflect the scale of the object on the z axis. I had no luck with the second answer - it scaled my gameobjects really huge, and its beyond my understanding to try to figure out what's going on.
For those interested in doing a similar thing, here's my finished code.
public float distance = 10;
public float goDepth = 4;
Vector3 v3ViewPort;
Vector3 v3BottomLeft;
Vector3 v3TopRight;
void Start () {
distance -= (goDepth*0.5f);
v3ViewPort.Set(0,0,distance);
v3BottomLeft = Camera.main.ViewportToWorldPoint(v3ViewPort);
v3ViewPort.Set(1,1,distance);
v3TopRight = Camera.main.ViewportToWorldPoint(v3ViewPort);
transform.localScale = new Vector3(v3BottomLeft.x-v3TopRight.x,v3BottomLeft.y-v3TopRight.y,goDepth);
}
Answer by Ren072 · Apr 14, 2014 at 11:35 AM
Here is the right answer for second approach: http://answers.unity3d.com/questions/491826/how-to-scale-a-plane-fit-it-screen.html
Answer by ShawnFeatherly · Sep 07, 2015 at 09:48 PM
I built off the code sample @ataxkt created. I encountered issues with the camera being rotated messing up all the calculations. I fixed that with this code.
public class ScaleToCamera : MonoBehaviour
{
public Camera camera;
void Start()
{
if (camera == null)
camera = Camera.main;
// ensure calculations are done when the camera is not rotated. Otherwise the z-axis will incorrectly have some depth
Vector3 camRotation = camera.transform.rotation.eulerAngles;
camera.transform.rotation = Quaternion.Euler(Vector3.zero);
// find corners of the cameras view frustrum at the distance of the gameobject
float distance = Vector3.Distance(this.transform.position, camera.transform.position);
Vector3 viewBottomLeft = camera.ViewportToWorldPoint(new Vector3(0, 0, distance));
Vector3 viewTopRight = camera.ViewportToWorldPoint(new Vector3(1, 1, distance));
// scale the gameobject so it touches the cameras view frustrum
Vector3 scale = viewTopRight - viewBottomLeft;
scale.z = transform.localScale.z;
transform.localScale = scale;
//return the camera to it's original rotation
camera.transform.rotation = Quaternion.Euler(camRotation);
}
}