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
2
Question by pborg · Aug 29, 2012 at 12:38 AM · guipositioncoordinates

how do I make a gui element appear in front of an object

I am soon to be reaching a playable phase of my game. I'm wondering how to create an icon that appears right next to or on top of an object. I understand how GUI buttons work and understand how to get the position. I've used ray cast to get the objects coordinates via the GUI coordinates now I need to do it backwards.

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
2
Best Answer

Answer by aldonaletto · Aug 29, 2012 at 12:58 AM

You could find the screen coordinates of a 3D world point with Camera.main.WorldToScreenPoint - but be aware that the Y coordinate runs upside down in the GUI system, thus you must invert it.
That's a simple script that shows the object name at offset world units above (script attached to the object):

var width: float = 100; // label width in pixels var height: float = 36; // label height in pixels var offset: float = 2; // offset above object in world units

function OnGUI(){ // calculate the world position for the label: var pos3d = transform.position + Vector3.up * offset; // convert it to screen space: var pos = Camera.main.WorldToScreenPoint(pos3d); // flip the Y coordinate vertically to match GUI space: pos.y = Screen.height - pos.y; // create a rect above pos: var r = Rect(pos.x-width/2, pos.y-height, width, height); // draw it: GUI.Label(r, transform.name); }

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 pborg · Aug 29, 2012 at 05:16 AM 0
Share

Thank you so much! That really helps :)

avatar image aldonaletto · Aug 29, 2012 at 12:44 PM 0
Share

I stupidly wrote "var OnGUI" ins$$anonymous$$d of "function OnGUI" - answer fixed 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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

GUI co-ordinates bug? 1 Answer

How to position elements on scene 0 Answers

Is gameObject present at coordinates (x,y,z)? 1 Answer

GUI.Window Set pos after runtime 0 Answers

Finding Coordinates of Objects in a List 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