- Home /
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.
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.
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
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).
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?
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
Hi! I try you solution, position x is work, but position y is invert. How can I fix it?
@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.
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
Your answer
Follow this Question
Related Questions
2D Gui Texture under 3D object 4 Answers
3d object on GUI 1 Answer
3D Object Position on GUI Screen 1 Answer
How to position 3D-GUI-Mesh on change of aspect ratio? 0 Answers
Making object fall over after death 1 Answer