Search Unity

[Solved]Getting text over players head

Discussion in '2D' started by kabuto178, Apr 18, 2017.

  1. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    Is it possible to get text to hover over a 2d characters head in game? If so, how could i get the text to follow the character? Do I use the canvas just the same?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If you need the text to be 3D (if you zoom in, the text maintains its relative size), you could make a World Space mode Canvas as a child of your character.

    If the text should always stay the same size on-screen, you can use Camera or Overlay mode Canvas, and write a script to convert the character position to screen space and update the text position on screen.
     
    Last edited: Apr 21, 2017
    kabuto178 likes this.
  3. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    and this is viable for a 2D game? the text I want to be over the NPC heads and the NPCs will be moving.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yep, both would work for your 2d game.
     
  5. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    Ok I gave it a try but no luck yet. Any example of implementing this so i can work from it?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Something like this:
    Code (CSharp):
    1. // on the character
    2. [SerializedField]
    3. Text mTextOverHead;
    4. Transform mTransform;
    5. Transform mTextOverTransform;
    6. void Awake() {
    7.    mTransform = transform;
    8.    mTextOverTransform =  mTextOverHead.transform;
    9. }
    10. void LateUpdate() {
    11.    Vector3 screenPos = Camera.main.WorldToScreenPoint(mTransform.position);
    12. // add a tiny bit of height?
    13. screenPos.y += 2; // adjust as you see fit.
    14. mTextOverTransform.position = screenPos;
    15. }
     
  7. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    I will go take a look, thanks for the tips. I will try to build off this
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If you use a world space canvas as a child of your character, then it will automatically follow the character around as a child object. You just have to size and position the canvas and child UI elements above the character.
     
    pixiepangolin likes this.
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yep, world canvas is cool, too. And in 2d (or 3d if you don't care about the size changing) they are probably pretty easy.
    Doesn't hurt to know a few options, though. :)
     
  10. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    What should the event camera be? a new camera instance?
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I don't know for sure, but if you aren't taking events on text over their heads, it can just be empty. If it does take events, try just the normal camera?
     
  12. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    Gotcha, it works. I was expecting some issues since it was 2D but once I set it up properly just needed to set the world mode and position as needed.