Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Hovering text over object

Discussion in 'Scripting' started by jessee03, Feb 20, 2015.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I'm trying to get my labels to stay above my objects but if I'm looking directly down or up at the object the text will be positioned too much below or above the object, same thing if I zoom the camera too far in and out. Also when attempting to attach this script to other objects the labels go all over the place and dont remain over the other objects correctly.

    Code (csharp):
    1.  
    2.  
    3.     public Vector3 offset = Vector3.up;
    4.     public Vector3 screenPos;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.         screenPos = Camera.main.WorldToScreenPoint(transform.position + offset);
    16.  
    17.     }
    18.  
    19.     void OnGUI ()
    20.     {    
    21.      
    22.         GUI.Label(new Rect(screenPos.x - 40,screenPos.y,100,50),"test", textStyle);
    23.         GUI.Label(new Rect(screenPos.x - 80,screenPos.y + 20,300,50),"" + "testtesttest", textStyle);
    24.     }
    25.  
    26.  
     
  2. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    I`ve noticed this too. What about attaching a gameObject above your object with a textmesh attached?
     
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I would rather do this all through code. Unity's new Gui system broke my old way I used to do this.