Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Frostraver · Nov 06, 2015 at 03:44 PM · cameraworldspaceconversionscreenspace

Conversion from world to screen space not right?

I'm having trouble showing a box at a certain position on the screen based on the position of an object in world space. I have a text-balloon GameObject above one of my NPC's and I want to write text in it by code.

I had this code to do that:

 Rect textFieldRect = new Rect(new Vector2(balloonPos.x, balloonPos.y), new Vector2(100, 100));
 Rect screenspacePos = new Rect(GameObject.Find("Camera").GetComponent<Camera>().WorldToScreenPoint(new Vector3(textFieldRect.x, textFieldRect.y, 0)), new Vector2(textFieldRect.width, textFieldRect.height));
 GUI.Box(screenspacePos, text);

but this doesn't seem to work as I expected. I can see the text showing up but it's not on top of the balloon as you can see in this screenshot: Imgur Link

I am probably doing something wrong with the conversion? The balloonPos data is right (I checked this).

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
1

Answer by Eric5h5 · Nov 06, 2015 at 11:58 PM

The conversion is correct, but your code can't work like that. The point of the conversion is that you put in a world coordinate and get a screen coordinate back, but you're making a new coordinate by stripping out the Z element, which is important. You need to supply a direct, unmodified world coordinate in order for the function to work.

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 Eno-Khaon · Nov 07, 2015 at 01:00 AM 1
Share

As an additional note, another likely problem is that the Y-axis is probably flipped. It may be necessary to use something like

 screenspacePos.y = (Screen.height - screenspacePos.y);

in order to get the correct vertical position.

avatar image DallasP9124 · Oct 05, 2020 at 07:37 PM 0
Share

I had the same problem recently with my custom camera rig. Zoo$$anonymous$$g out would misaligne the point but using @$$anonymous$$o-Khaon 's note fixed the issue.

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

6 People are following this question.

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

Related Questions

Recreating basic function in C# 1 Answer

Is there an alternative to Camera.WorldToScreenPoint or another way to solve this problem? 0 Answers

How to prevent image from rendering to screen but seen by camera 0 Answers

Pixel-to-meter camera 1 Answer

how to get the most right point or left point atc of object as visual on the screen in 3d project? 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