- Home /
Scale 3D object according to screen size on orthographic camera
I am working with 3D objects in 2D orthographic view. Now I want to scale objects or in my case, a cylinder according to the screen size, such that it keeps a certain height and shape for any screen size. I tried with ScreenToWorldPoint but it gives unmeasurable results. What would be the proper way to do this?
Edit: I also want to scale the object in relation to screen with or height, let's say for example, the cylinder's height will be one sixth of the screen height and I need to be able to do this in the code. The height will change according to level number and screen height. Like, in level 6, the height will be Screen.height/6, also radius will be scaled to keep the ratio.
What do you mean by
keeps a certain height and shape for any screen size.
In relation to width or height ? Or so that it's always e.g. 2cm wide on the screen ?
The biggest problem with these screen size question is very often that even the person asking the question doesn't quite know what he wants because the problem has so many moving parts :)
Answer by NoseKills · Dec 14, 2014 at 02:48 PM
If you are going to change both the height and width of the GameObject, you can't do this only by changing the camera size. Because of this I think it's easier to scale the object although in other cases it might be easier to change the camera size.
So like the orthographic camera API doc says, the orthographicSize is half of the viewing volume size.
You can get the visible area dimensions in world units like this:
float height = Camera.main.orthographicSize * 2;
float width = height * Screen.width/ Screen.height; // basically height * screen aspect ratio
Now you just have to scale your GameObject to 1/6 of the height or width or both or whatever you want.
gameObject.transform.localScale = Vector3.one * height / 6f;
Or vise-versa also works for me. But this should be the correct answer. Thanks.
Edited the answer. I had accidentally flipped Screen.width/height
Sorry for writing in old post. Tried the same as explained here, but with this line,
float width = height * Screen.height/ Screen.width;
It seems worked for me.
Not sure its right or wrong, as per doc its "aspect ratio = width/height",
float width = height * Screen.width/ Screen.height;
but this doesn't seem to work for me. Stays in specific height in all the aspect ratios. Entire code is,
float height = mCamera.orthographicSize * 2.0f;
float width = height * Screen.height / Screen.width;
gameObject.transform.localScale = Vector3.one * width / SIZE_ON_SCREEN;
SIZE_ON_SCREEN = 5. Orthographic camera size set to 5.
Please let me know, if anything wrong with this code/approach. Thanks in advance.
Tried with the aspect ratios, 2732x2048[Landscape], 1920x1080[Landscape] and 960x540[Landscape].
Answer by 0mr_ashutosh0 · Dec 11, 2014 at 06:24 PM
@Nafis A Khan
Here is how you deal with this.
First of all this works only on ortho camera.
1) you dont change your objects YOU CHANGE THE SIZE OF CAMERA.
2) lets say the cube you created has a scale of 1,1,1 and in camera the size is by default set to 5 then from left to right you can fit 5 such cubes adjacent to each other.
in order to make your screen responsive to other various devices use this line in 'Start' function of any script which is used throughout the game
Camera.main.orthographicSize = (20.0f / Screen.width * Screen.height / 2.0f);
http://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html
Here the width of your camera is 10 according to the device's pixel information.
Experiment: Try this paste this line in Update method and then click on your camera and now resize your 'game' scene and notice 'size of your camera, you will know.
What if I want to keep my cylinder's height one sixth of Screen height? What should I change here?
camera the size is by default set to 5 then from left to right you can fit 5 such cubes adjacent to each other.
This is not correct. Orthographic size is half of the viewport height. If it's 5, it means the view is 10 units high and the width depends on screen aspect ratio
Camera.main.orthographicSize = (20.0f / Screen.width * Screen.height / 2.0f);
Doesn't make any sense to take screen width into account when the orthographic size is only dependent on the vertical size like the API says
Experiment: Try this paste this line in Update method
Why Update method ? There shouldn't be any need to constantly do this if the object sizes don't change during one level.
@Nose$$anonymous$$ills, that means, I have to change the orthographicSize to 6 if I want my cylinder height be one sixth of screen height? Assu$$anonymous$$g cylinder's y scale is 1.
You'd have to change it to 3. Orthographic size is half of screen height, so 3 would make the screen 6 world units high, thus making a 1 unit object 1/6 of it.
@Nose$$anonymous$$ills, thanks a lot. BTW, It would be 6 not 3 cause scaling factor of an object is also half of the world unit. But I get it. :)
Your answer
Follow this Question
Related Questions
UI Image Scaler 0 Answers
Orthographic camera size and object scale 0 Answers
¿Is it possible to scale independently of the rotation? 0 Answers
How To Spawn GameObject In Top-Left Corner In 3D Space? 1 Answer