Search Unity

NPC Name Over Head

Discussion in 'Immediate Mode GUI (IMGUI)' started by QuinnH, Dec 3, 2009.

  1. QuinnH

    QuinnH

    Joined:
    Nov 30, 2009
    Posts:
    5
    What are the recommended ways of putting the NPC's name over their head?

    I tried a GUI.Label but that doesn't work as the Text over the head should scale with distance, ie, the further away you are, the smaller the text should be.

    I have searched the forums and haven't found a good a way to do this. Do I have to change fonts based on the distance from the camera?

    Thanks
     
  2. dbryson

    dbryson

    Joined:
    Nov 9, 2009
    Posts:
    269
    Check out the code provided by AaronC in another thread:

    This code uses a GUIText.
     
  3. QuinnH

    QuinnH

    Joined:
    Nov 30, 2009
    Posts:
    5
    I tried downloading that project but it says it is an invalid zip file.
     
  4. dbryson

    dbryson

    Joined:
    Nov 9, 2009
    Posts:
    269
    Yeah, I had that problem too. I used the InfoZip utilities to unzip it.
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You might also be able to use a TextMesh to display the names. TextMeshes are objects in 3D space, so they can be placed above the character and will scale as appropriate.
     
  6. QuinnH

    QuinnH

    Joined:
    Nov 30, 2009
    Posts:
    5
    Thanks!

    Used a TextMesh ( 3D Text ) to show NPC names.
     
  7. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    How do you lock the text mesh to always face the player?
     
  8. QuinnH

    QuinnH

    Joined:
    Nov 30, 2009
    Posts:
    5
    using UnityEngine;
    using System.Collections;

    public class NPCName : MonoBehaviour
    {
    Transform nameText = null;

    // Use this for initialization
    void Start()
    {
    this.nameText = transform.Find("NameText");
    }

    // Update is called once per frame
    void Update()
    {
    if (this.nameText != null)
    this.nameText.forward = Camera.mainCamera.transform.forward;
    }
    }

    I tried just doing a Transform.LookAt(camera), but the text was always backwards. Seems like to me the forward of the 3d text is backwards.

    I got it to work with my hack. Anyone have the right way to do it?
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Check out this thread for a code snippet to make transform.LookAt look in the opposite direction.
     
  10. Dabookman

    Dabookman

    Joined:
    Oct 20, 2009
    Posts:
    17
    If your not doing to much stuff with it and you want to use the LookAt script, I'd suggest scaling the text mesh in negative X and Z. Had the same problem myself but this fixed it right away, and the selecting of the mesh wasn't affected either.
     
  11. wisly

    wisly

    Joined:
    Jan 18, 2010
    Posts:
    12
    yes,It works fine.
     
  12. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Heya,

    To the windows users who had trouble downloading the zip, my apologies. I just reloaded the file- I dont know if its any good but if anyone feels like checking that would be great. Its about 3mb

    http://deepwater3d.com/DW3D/Realtime/MG_Advanced.zip

    Thanks
    AaronC
     
  13. DanNZ

    DanNZ

    Joined:
    Mar 28, 2013
    Posts:
    3
    on unity it says to insert a semicolon at line1, line2, line6
    <br> and its says that : is an unexpected character
    <br> .
    <br> Why?
     
  14. AlchemistDK

    AlchemistDK

    Joined:
    Oct 15, 2014
    Posts:
    3
    I use this, if you want you can't remove the "public" from Camera.
    :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NombrePersonaje : MonoBehaviour {
    5.  
    6. public string Nombre;
    7. public Camera cam;
    8. TextMesh texto;
    9.  
    10. // Use this for initialization
    11. void Start () {
    12. cam = Camera.main;
    13. texto = GameObject.Find("Nombre_Char").GetComponent<TextMesh>();
    14. texto.text = "<"+Nombre+">";
    15. }
    16.  
    17. // Update is called once per frame
    18. void Update () {
    19. if (cam != null) {
    20. transform.LookAt (cam.transform);
    21. }else
    22. {
    23. cam = Camera.main;
    24. }
    25. }
    26. }
    27.  

     
    Last edited: Oct 27, 2014