- Home /
Name above object C#
I'm trying to get a persistent name label above certain objects within my game. At the moment, I'm trying to use http://www.unifycommunity.com/wiki/index.php?title=ObjectLabel but it seems that the label continually moves and also appears when I turn the camera to face away from the object.
What I'm trying to get is a label that doesn't move from the position above the object/character. I'm really stumped and any help would be appreciated :)
Would it be better/easier to use a textmesh/object above the NPC (object)? If so, how?
Answer by aldonaletto · Jul 09, 2012 at 02:40 AM
You can check if the object isn't behind the camera using a simple dot product between the camera's forward direction and the direction camera->object - a negative dot product means the object is behind the camera.
... void Update(){ if (Vector3.Dot(camTransform.forward, target.position-camTransform.position) < 0){ // object behind camera: guiText.enabled = false; // make label invisible... return; // and abort further calculations } guiText.enabled = true; // otherwise make label visible
if (clampToScreen){ // rest of Update unmodified
...
Okay, that fixed the issue of it appearing when it shouldn't, but the label is still moving about randomly when I move my camera... http://db.tt/QrAgpZCz is a screenshot of my Unity showing the offending GUI Text and its respective settings.
The GUIText must not be childed to anything - drag it out of the Cylinder object and the ObjectLabel will keep it s$$anonymous$$dily attached to the target. GUIText and GUITexture must never be childed to 3D objects, because their position fields must be in the 0..1 range, and any parent movement kicks the GUI element out of screen.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
how to put in function any variables? like object? 2 Answers
Rotate the Object 90 Degrees 1 Answer