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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to Get World Space Text to Look at The Player

Discussion in 'Scripting' started by Hotpots, Apr 28, 2016.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    So what I would like to do is to get my world space text to ALWAYS face the player, therefore they can read the text no matter what angle they walk up to the text from.

    However the problem I am having is that the text is being portrayed as it would in a mirror, thus what I would like to do is rotate it 180 degrees whilst also looking at the player.

    Here is the script I have at the moment:

    Code (CSharp):
    1.     public Transform target;
    2.  
    3.     void Update()
    4.     {
    5.         if(target != null)
    6.         {
    7.             transform.LookAt (target);
    8.         }
    9.     }
    So at the moment the text is perfectly looking at the target, but how would I also rotate the text on the y axis?

    My quick attempt was this, but at no avail:

    Code (CSharp):
    1. transform.LookAt (target.localPosition, target.localRotation.y + 180, target.localScale);
    Any help is appreciated! :)
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    LookAt changes the rotation so the object's looking at the point the player's standing in world space. Your quick attempt is using a mix of the player's local position and the y-component of the player's rotation to look at something that's nonsense, and almost certainly 180 meters above anything interesting.

    There's two good solutions here:
    - Look at the player, and then rotate 180 degrees:
    Code (csharp):
    1. transform.LookAt(target);
    2. transform.rotation *= Quaternion.Euler(0, 180, 0);
    Update happens before things are drawn to the screen, so the 180-degree back and forth won't be visible.

    - Fix this in the text gameObject:
    Use the code as you have it now, but put the text as a child object, rotated 180 degrees relative to the parent.
     
    Hotpots likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2.  public Transform target;
    3.  
    4.     void Update()
    5.     {
    6.         if (target != null)
    7.         {
    8.             transform.LookAt(transform.position-target.position);
    9.         }
    10.     }
    11.  
    (fixed it)
     
    Hotpots likes this.
  4. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Thank you very much guys, both of your solutions worked :)

    Hopefully I can solve something simple like this myself next time :)


    P.S. Damn Quaternions...
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    @LeftyRighty, that's a bit too clever. "Look at something at the exact opposite side of me as the player"?

    I like it.
     
    LeftyRighty likes this.