• 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 Sealmeister · Jun 13, 2015 at 12:42 PM · understandunderstanding logic

Can somebody explain the math behind this?

I was trying to get a GUI texture hovering above an object's position. My problem was that the texture was changing in the y-axis while being placed correctly in the x-axis. After hours of trials, errors and searching the web I finally managed to get it right.

 Vector3 p = Camera.main.WorldToScreenPoint (gameObject.transform.position);
         p.y = Screen.height - p.y;
         GUI.DrawTexture(new Rect((p.x + offsetX), (p.y + offsetY), (0 + width), (height + height)),ttext);

Apparently I did not have the "p.y = Screen.height - p.y" in my first examples. With it it works like a charm. My question is why "p.y = Screen.height - p.y" did the trick? Can someone explain what that piece of code actually does? Why is it only necessary for the y-coordinate and not for the x like "p.x = Screen.width - p.x"? I would really appreciate if someone could explain this to me as I have no idea why it works now...

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dave-Carlile · Jun 13, 2015 at 12:46 PM

The documentation is kind of odd on this - one place says that screen space y starts with 0 at the bottom, and another that starts with 0 at the top. I can never remember and have to figure it out each time. I believe the former is correct.

Regardless, you're dealing with two different spaces here. WorldToScreenPoint gives you screen space, but the GUI drawing apparently works in GUI space where the y is flipped - i.e. 0 at the bottom for screen space, 0 at the top for GUI space. So you have to transform between the spaces.

You should be able to use the GUIUtility.ScreenToGUIPoint to transform your screen space point into GUI space, rather than flipping y yourself.

Comment
Add comment · Show 2 · 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 Bunny83 · Jun 13, 2015 at 12:56 PM 1
Share

Yes, the documentation on GUIUtility.ScreenToGUIPoint uses the wrong term. Not the screen space has it's origin at the top but the GUI space.

Or if they wanted to point out that the screen space has it's origin at the bottom they just messed up the sentence ^^.

Either way it's wrong as it's written on the ScreenToGUIPoint page.

Btw: Even B$$anonymous$$P files usually store the image upside-down. At hardware level the origin is usually always at the bottom left

avatar image Sealmeister · Jun 13, 2015 at 02:54 PM 0
Share

I see, thank you!

avatar image
1

Answer by Bunny83 · Jun 13, 2015 at 12:48 PM

Well, that's simple ^^. The Screen-space which Unity (any most other engines uses) has the origin at the bottom left corner of the screen. It's the usual device coordinate system. So the x-axis goes from left to right and the y-axis goes from bottom to top.

The GUI-space however has it's origin at the top left corner. So if you want to convert a screen space position into GUI space you just have to subtract the position from the screen height so you know where the point is in GUI space.

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 Sealmeister · Jun 13, 2015 at 02:53 PM 0
Share

Thank you! I understand now :)

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Logical Error In Spawn Counter (Javascript) 2 Answers

Basics of Perlin Noise? 1 Answer

Mesh information explained. 1 Answer

JUMP LOGIC 2 Answers

Voxel editing in unity3d. 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