• 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
0
Question by boymeetsrobot · Dec 05, 2010 at 09:47 PM · 2dflashlocalscale

Help with transition from 2d programming with pixel based co-ordinates

I come from a background of coding flash applications for many years and I'm finding it hard to do things that were previously very simple with 2d pixel based space in Unity. I was hoping for some input on how one would go about doing them in 3d.

The lack of width and height relative to the screen is the main thing I am struggling to come to terms with. For example, in flash I could make an object fill the screen by simply doing:

// Make object size of the screen
myObject.width = stage.stageWidth;
myObject.height = stage.stageHeight;

Or I could center an object by doing:-

// Center an object in the middle of the screen
myObject.x = stage.stageWidth / 2 - myObject.width / 2;

I have worked out that localeScale and localePosition can be used for some things. If someone could post how they would commonly perform these tasks in Unity, along with any other examples, it would help me a lot.

Thanks

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 yoyo · Dec 05, 2010 at 10:26 PM 0
Share

Screen.width/height are similar, does that help? http://unity3d.com/support/documentation/ScriptReference/Screen-width.html

avatar image boymeetsrobot · Dec 05, 2010 at 11:58 PM 0
Share

How does one scale an object to fit the screen dimensions or work out screen.width in relation to the world space? Even make one object the same size of another? Thanks for your help.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Wolfram · Jun 23, 2012 at 12:22 PM

Several possibilities:

  • You can limit yourself to GUIText, GUITexture, or the UnityGUI (=anything that goes in OnGUI(), for example GUI.Button(), GUI.Label, etc.). However, this way you restrict yourself to non-3D objects

  • With perspective cameras, objects need to be larger if they are further away, to have the same pixel size. To avoid that, you can just make your camera orthographic. This way, it will have no perspective distortion, objects are always the same size, no matter how far form the camera they are. The height of the screen in world coordinates is just camera.orthographicSize*2

  • For individual points, you can use the Approach @DelStrega mentioned, using ViewportToWorldPoint(), or better ScreenToWorldPoint(), which uses pixel values

  • The most flexible and powerful approach is to use a (or several) parallel plane to the camera, which is positioned in a way that world coordinates are directly transformed into screen pixels or a normalized viewport space. See my approach here for example on how to accomplish this. The two related main formulas connected to this are:

      // required distance object<->camera so that object exatly fills
    
      // (vertical) screen height:
    
      cameraZ=0.5/Mathf.Tan(Camera.main.fieldOfView/2*Mathf.Deg2Rad)*
    
              heightOfYourObjectInWorldCoordinates
    
      // required object height in world coordinates at given distance
    
      // so that camera sees it at a certain pixel height
    
      heightOfYourObjectInWorldCoordinates=
    
          2*Mathf.Tan(Camera.main.fieldOfView/2*Mathf.Deg2Rad)*
    
          objectDistanceFromCamera*desiredPixelHeight/Camera.main.pixelHeight
    
     
    

(NOTE: Camera.main.pixelHeight is the same as Screen.height, if your camera fills the whole screen (as opposed to for example a picture-in-picture camera)

EDIT before even posting: I just realized that @DelStrega revived this ancient question from 2010, which is generally not a good idea. But that was when I finished typing, so I'll post my answer anyway.

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 delstrega · Jun 23, 2012 at 12:38 PM 0
Share

Wow, sorry, didn't realize that actually. I just clicked on "Unanswered" and this came up on the first page. Didn't check the date - my bad :(

avatar image whydoidoit · Jun 23, 2012 at 01:27 PM 0
Share

It does that sometimes just to keep us on our toes:) @wolfram where do you get the year from? I just see month and day...

avatar image Wolfram · Jun 23, 2012 at 01:31 PM 0
Share

@DelStrega - no harm done ;-) I guess the original idea is of course to answer all unanswered questions, but with these old questions it is rather unlikely that it will help the original poster. But of course it might still help people who stumble upon this question, or search for a topic like this!

@whydoidoit - it's rather tricky and inconvenient, it might be a problem of the QATO engine itself. "Just" use mouse-over, to get a tool-tip with the full date ;-)

avatar image
1

Answer by delstrega · Jun 23, 2012 at 11:52 AM

I use something like this to achieve what you are trying to do:

public Camera cam;

 ...
 
 Vector3 camUpperLeft = cam.ViewportToWorldPoint(new Vector3(0,1,-cam.transform.position.z));
 Vector3 camUpperRight = cam.ViewportToWorldPoint(new Vector3(1,1,-cam.transform.position.z));
 Vector3 camLowerRight = cam.ViewportToWorldPoint(new Vector3(1,0,-cam.transform.position.z));
     
 float viewWidth = Mathf.Abs(camUpperLeft.x - camUpperRight.x);
 float viewHeight = Mathf.Abs(camUpperLeft.y - camLowerRight.y);

...

Now, if your object has a scale of (1, 1, 1) by default you can just use viewWidth and viewHeight to adjust it's scale to fill the screen.

obj.transform.localScale = new Vector3(viewWidth, viewHeight, 1);

Hope that helps.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Trying to make my Player's sword knockback the enemy without making the enemy animation freak out. 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Help Wanted. Switching localscale starts unwanted animation state. 1 Answer

2D Input.GetAxis not recognized when local scale is flipped 0 Answers

What would It take to make a game like this? 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