Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by CABRINx86 · Aug 18, 2013 at 06:43 PM · positioncoordinatesnguiworldtoscreenpoint

WorldToScreenPoint result of Player Object position with bad Y/Z values

Hi,

I'm building a 3D 3rd person game, using NGUI for UI.

I have several objects the player can interact with, displaying a verb and then a description if the player chooses to interact.

Aesthetically, I want the UILabel to always appear next to the player's position in the world, so I'm using a "WorldToScteenPoint" calculation to translate the player's 3D position into screen-space to be used by the 2D GUI.

The problem is, that every time this calculation is done, the Y position is decrementing and the Z position is incrementing (the Z doesn't make a difference to the appearance, but the Y changes mean that the label soon appears off-screen).

Here is the output of the Debug Prints of the calculated 2D coordinates, and the player's 3D position, just by moving player left-right in and out of trigger:

myScreenPos = (792.8, 302.7, 9.1) 3D Pos = (-0.2, -0.5, -1.4) myScreenPos = (802.4, 240.7, 9.6) 3D Pos = (-0.1, -1.5, -1.4)

myScreenPos = (792.7, 195.7, 10.0) 3D Pos = (-0.2, -2.3, -1.4)

myScreenPos = (793.9, 170.4, 10.2) 3D Pos = (-0.2, -2.7, -1.4)

myScreenPos = (794.1, 151.1, 10.4) 3D Pos = (-0.2, -3.1, -1.4)

myScreenPos = (793.8, 130.5, 10.6) 3D Pos = (-0.2, -3.5, -1.4)

myScreenPos = (800.1, 112.1, 10.8) 3D Pos = (-0.1, -3.9, -1.4)

myScreenPos = (792.5, 93.9, 11.0) 3D Pos = (-0.2, -4.2, -1.4)

myScreenPos = (799.1, 26.4, 11.8) 3D Pos = (-0.1, -5.8, -1.4)

And here is the script that calculates it: (Temporarily, in the Movement script I've created a Static GameObject for the Player, so that I can reference its position in other scripts)

 void OnTriggerEnter(Collider myCollider)
     {
         if (myCollider.CompareTag("Player"))
         {
             InputManager.instance.ToggleInteractableBool(true, thisScript);
 
             interactLabel.text = textLabel;
             
             Vector3 playerPos = new Vector3(Movement.playerObject.transform.position.x, Movement.playerObject.transform.position.y, Movement.playerObject.transform.position.z);
             myScreenPos = Camera.main.WorldToScreenPoint(playerPos);
             Debug.Log("myScreenPos = " + myScreenPos + "\n3D Pos = " + playerPos);
             
             interactPanel.transform.localPosition = myScreenPos;
             alphaTweener.Toggle();
  
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Benproductions1 · Aug 18, 2013 at 11:05 PM

Hello,

The function WorldToScreenPoint returns the World coordinate in screen space. For purposes of simplification, Unity's GUI (and I'm guessing NGUI too) does not use the standard screen space. In the GUI's screen space, y=0 is at the top of the screen, while in the real screen space y=0 is at the bottom. To fix this you need to do a simple calcuation:

 screenPos.y = Screen.height - screenPos.y;

Also note that the Z value represents the distance from that point to the virtual "screen" in world space. This is sometimes usefull if you want to scale the GUI depending on distance. All this and more can be seen in the documentation if you want any more information ;)

As a last note, on line 9 in your code, you should have:

 Vector3 playerPos = Movement.playerObject.transform.position;

And not the horror you currently have ;)

Hope this helps,
Benproductins1

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

16 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 avatar image avatar image

Related Questions

Position set way off / Different coordinate system problem 0 Answers

NGUI positioning over NPC 0 Answers

Event System value of mouse position is wrong. 0 Answers

split vector3 coordinates in to x,y,z 1 Answer

AngryBots mobile joysticks position 0 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