- Home /
scale plane to fit to screen size
Hello everyone,
I have an orthographic camera of size 100, and I have a plane that I want to fit/scale according to the screen size. To be more clear, I want the functionality of a guiTexture on a plane/mesh.
In the case of a guitexture, I would have set transform.postion = vector3(0,0,0) and play with the values of pixelInset like:
myGuiTexture.pixelInset = new Rect(0,0,Screen.currentResolution.width,Screen.currentResolution.height);
So how to achieve something like this with transform.position or any other method to do this with 3D object. I want this since I am using tk2d and I want my sprite animation to fill the whole screen.
Thanks,
Answer by robertbu · Apr 24, 2013 at 03:06 PM
Since you are using an orthographic camera, from the reference:
[OrthographicSize] is half of the vertical size of the viewing volume. Horizontal viewing size varies depending on viewport's aspect ratio.
So you will set the vertical size of your plane to twice the orthographic size and then scale the horizontal based on the ratio of Screen.width / Screen.height.
@robertbu thank you for your support.. Here's my script:
transform.localScale = new Vector3(cam.orthographicSize/2 * (Screen.width/Screen.height),cam.orthographicSize/2,0f);
but its not fitting perfectly, the width is smaller than screen width (there are narrow vertical columns on both sides), and the height is more than Screen.height.. what might be my fault?
Your calculations are wrong. Attach this to a standard cube to verify the code:
function Start () {
var height = Camera.main.orthographicSize * 2.0;
var width = height * Screen.width / Screen.height;
transform.localScale = Vector3(width, height, 0.1);
}
Now you need to verify some some things about the plane you are using. First how big is it? The standard plane in unity is 10x10 when scaled to (1,1,1). And what is the orientation? For the standard plane you will need to modify the Z parameter rather than the y parameter. To get a plane the right size and orientation you can use the CreatePlane script.
@robertbu thank you again, but I am using 2DToolkit, and I have a sprite , how to know how big it is? Its scale is (1,1,1) ..
I don't know 2DToolkit, so I don't know how they size things. Do they base size and/or aspect based on the texture, or do you enter the size as part of a script? As a starting point, take a sprite with a square texture and compare it to a thin cube.
This Script is not working for me the plane is musch bigger then the screen.
Try this c# code
float height = (float)Camera.main.orthographicSize 2.0f; float width = height Screen.width / Screen.height; transform.localScale = new Vector3(width/10, 1.0f, height/10);
Your answer
![](https://koobas.hobune.stream/wayback/20220613100925im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object not centering with screen.width/2 0 Answers
Spawn objects at bottom of screen / camera 2 Answers
Unity Camera and transform? 1 Answer
screen space camera hud (4.6 beta) 2 Answers
Multiple Targets Camera 1 Answer