Search Unity

Keeping UI Text above Character in 2D Coordinates

Discussion in 'Scripting' started by aviles622, Mar 2, 2015.

  1. aviles622

    aviles622

    Joined:
    Oct 16, 2014
    Posts:
    21
    I don't know if I exactly worded my question correctly but here is my issue. I have a multiplayer game where all the players tagname is displayed hovering over its character object in 3D. The hover text is a UI text object in world space. The text is a child of the character object and is always above the character.

    How can make this text stay in 2D coordinates and not rotate when the character rotates. Want to make the text just always face the screen without rotating...


     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Considering you are using Canvas ;

    Not sure how Canvas is going to react to simple transform events, but I have a couple of ways in my mind that you can try :

    - Use LookAt to face the transform of the UI to player.

    Code (CSharp):
    1. private Transform player;
    2.  
    3. void Start()
    4. {
    5. player = GameObject.FindGameObjectWithTag("Player").transform;
    6. }
    7.  
    8. void Update()
    9. {
    10. Vector3 lookAtPos = player.position;
    11. lookAtPos.y = transform.position.y; // this will make the UI to rotate only X-Z axis according to player.
    12. transform.LookAt(lookAtPos);
    13. }
    - Use Quaternion Look Rotation to face the transform of the UI to player. Which is given with a good example over reference page.
     
    honprovet likes this.
  3. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    I did that yesterday, but im not home noW so i cant copy the code.

    The idea is that you should rotate the UI object transform, via code, so that it always faces the camera. Its not hard, try it.
     
  4. aviles622

    aviles622

    Joined:
    Oct 16, 2014
    Posts:
    21
    Thank you both. Gonna try this now...