• 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 DevonJavaScript · Oct 05, 2011 at 08:11 PM · gui3dobjectoverattached

GUI Label follow an object?

So I'm working on a 3D Menu sort of thing but its in game so I would like it to move with the character in the sense a single label is connected to a certain part of the 3D Object.

So how would I go about finding the pixel offsets in 3D Space to 2D for GUI Text, buttons and labels. I know how to make GUI components but not find the position of the 3D Objects to 2D for the GUI.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kegrah · Oct 05, 2011 at 10:47 PM

I'm a little unclear as to what you're looking for, it sounds like you are trying to have a floating billboard above a 3D object in game?

If so just have a flat object as a child of that object and have the transform.rotation be set to that of the camera, this should make it always turn to face the camera and be readable. To interact with it you could have the camera RayCast out from the camera to the mouse position, if it hits that flat object it then executes whatever your intended menu item is.

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

Answer by Hendrys · Oct 05, 2011 at 09:54 PM

You should use Camera.WorldToScreenPoint() it is a method that you can use to transform the position of your character to the respective point (in pixels) in the screen.

Check: http://unity3d.com/support/documentation/ScriptReference/Camera.WorldToScreenPoint.html

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

Answer by aldonaletto · Oct 05, 2011 at 08:58 PM

To convert a world point to the screen space, use WorldToScreenPoint:

 var target: Transform; // drag the target object here
 var rect = Rect(0,0,300,100);
 var offset =  Vector2(0, 1.5); // height above the target position
 
 function OnGUI(){
     var point = Camera.main.WorldToScreenPoint(target.position + offset);
     rect.x = point.x;
     rect.y = Screen.height - point.y - rect.height; // bottom left corner set to the 3D point
     GUI.Label(rect, target.name); // display its name, or other string
 }

This is ok for the GUI system, which uses screen coordinates. If you want to use GUIText or GUITexture, you must find the point with WorldToViewportPoint, which returns x and y in the range 0..1 (viewport coordinates).

Comment
Add comment · Show 5 · 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 DevonJavaScript · Oct 05, 2011 at 10:12 PM 0
Share

This does not work for me.. I'm testing on a cube and attatching the script to the main Camera. I also tried switching point to WorldToViewpoerPoint but that doesn't give me any result either. Any help?

avatar image aldonaletto · Oct 05, 2011 at 11:51 PM 0
Share

No problem, just drag the cube to the target variable: this script makes the GUI.Label to appear at offset meters of the target's position - by the way, I added the offset to the screen coordinates, what is wrong - I fixed the answer

avatar image Jack31081988 · May 01, 2014 at 06:33 PM 0
Share

O$$anonymous$$G! Thank you so much..

avatar image boboyon · Nov 13, 2014 at 02:16 AM 0
Share

Hi! I try you solution, position x is work, but position y is invert. How can I fix it?

avatar image aldonaletto · Nov 14, 2014 at 12:09 PM 0
Share

@boboyon, you're right: Y is 0 at the top in the GUI system. We must subtract the desired coordinate from the window height in order to get the correct GUI Y. The answer has been fixed, and is ok now.

avatar image
0

Answer by NoobStudios · Nov 25, 2011 at 09:16 PM

Use This. It worked for me. http://www.unifycommunity.com/wiki/index.php?title=ObjectLabel

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

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

8 People are following this question.

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

Related Questions

2D Gui Texture under 3D object 4 Answers

3d object on GUI 1 Answer

3D Object Position on GUI Screen 1 Answer

Trying to Destroy Object when Wave starts 1 Answer

[CLOSED] Render priority of gameObjects 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges