Search Unity

3d multiplayer style nametags above character, How?

Discussion in 'Immediate Mode GUI (IMGUI)' started by LaSalle, Feb 16, 2011.

  1. LaSalle

    LaSalle

    Joined:
    Nov 7, 2009
    Posts:
    7
    Ive searched for a long while on multiple forums and all I find are the 2d style name tags which don't behave as I would expect.

    Im looking for mmo style nametags/titles that appear above the character in 3d space.

    Can someone point me in the right direction?

    here is the 2d script Im currently using..
    Code (csharp):
    1.  
    2.  
    3. var target : Transform;  // Object that this label should follow
    4.  
    5. var offset = Vector3.up;    // Units in world space to offset; 1 unit above object by default
    6. var clampToScreen = false;  // If true, label will be visible even if object is off screen
    7. var clampBorderSize = .05;  // How much viewport space to leave at the borders when a label is being clamped
    8. var useMainCamera = true;   // Use the camera tagged MainCamera
    9. var cameraToUse : Camera;   // Only use this if useMainCamera is false
    10. private var cam : Camera;
    11. private var thisTransform : Transform;
    12. private var camTransform : Transform;
    13.  
    14. function Start () {
    15.     thisTransform = transform;
    16.     if (useMainCamera)
    17.         cam = Camera.main;
    18.     else
    19.         cam = cameraToUse;
    20.     camTransform = cam.transform;
    21. }
    22.  
    23. function Update () {
    24.     if (clampToScreen) {
    25.         var relativePosition = camTransform.InverseTransformPoint(target.position);
    26.         relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
    27.         thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
    28.         thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
    29.                                          Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
    30.                                          thisTransform.position.z);
    31.     }
    32.     else {
    33.         thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
    34.     }
    35. }
    36.  
     
    Last edited: Feb 18, 2011
  2. LaSalle

    LaSalle

    Joined:
    Nov 7, 2009
    Posts:
    7
    Solved: added a text mesh object as a child of my character and positioned/styled it...
    added the following camera facing billboard script to the text mesh:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraFacingBillboard : MonoBehaviour
    6. {
    7.     public Camera m_Camera;
    8.  
    9.     void Update()
    10.     {
    11.         transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
    12.             m_Camera.transform.rotation * Vector3.up);
    13.     }
    14. }
    15.  

    seems to work well for now.