Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by ataxkt · Jan 31, 2013 at 07:23 PM · gameobjectscaleviewportstretch

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!

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 archaismic · Feb 01, 2013 at 08:00 AM 0
Share

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

avatar image zfz.zahid · May 17, 2014 at 10:02 AM 0
Share

did u got any correct method so that gameobjects scale and position with different screen sizes?

4 Replies

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

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;

Comment
Add comment · Show 1 · 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 stanete · Mar 07, 2015 at 11:40 AM 0
Share

It helped a lot. Thank you.

avatar image
3

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);
     }
Comment
Add comment · Show 1 · 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 msakkuzu · Dec 26, 2013 at 10:26 AM 0
Share

Thanks a lot, nice to share your solution.

avatar image
1

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

Comment
Add comment · 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
0

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);
     }
 }
Comment
Add comment · 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

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to align the bottom of a GameObject with the ground? 1 Answer

How to change child's rotation whithout affecting it's scale? 1 Answer

Make long distance objects visible on camera 0 Answers

Edit multiple assets at the same time? 2 Answers

Instantiating Cube Prefabs with scale 3 Answers


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