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.
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;
@Nafis A Khan
Here is how you deal with this.
First of all this works only on ortho camera.
-
you dont change your objects YOU CHANGE THE SIZE OF CAMERA.
-
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);
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.