Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by NafisKhan · Dec 11, 2014 at 06:02 PM · 3dscalescreen sizeorthographic camera

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Dec 11, 2014 at 05:59 PM 0
Share

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 :)

avatar image NafisKhan · Dec 11, 2014 at 07:29 PM 0
Share

Sorry for not making it clear. I edited my question.

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NafisKhan · Dec 14, 2014 at 02:52 PM 0
Share

Or vise-versa also works for me. But this should be the correct answer. Thanks.

avatar image NoseKills · Dec 14, 2014 at 05:57 PM 0
Share

Edited the answer. I had accidentally flipped Screen.width/height

avatar image achillesdotexe NoseKills · Mar 15, 2016 at 12:39 PM 0
Share

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].

avatar image
3

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NafisKhan · Dec 11, 2014 at 07:39 PM 0
Share

What if I want to keep my cylinder's height one sixth of Screen height? What should I change here?

avatar image NoseKills · Dec 14, 2014 at 02:31 PM 0
Share

@0mr_ashutosh0

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.

avatar image NafisKhan · Dec 14, 2014 at 02:40 PM 0
Share

@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.

avatar image NoseKills · Dec 14, 2014 at 02:52 PM 0
Share

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.

avatar image NafisKhan · Dec 14, 2014 at 02:55 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

Scale gameobject to the width of screen 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges