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

3D text face the player.

Discussion in 'Scripting' started by rennster200, Mar 27, 2022.

  1. rennster200

    rennster200

    Joined:
    Jun 21, 2019
    Posts:
    65
    Hello, I found tutorial on how to make object always look at certain target. In this case I created TMP 3D text and attached the script. Text is always looking at my player but 180 degrees rotated, like my player sees the text written backwards.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookAt : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     private void Update()
    9.     {
    10.  
    11.         Vector3 direction = target.position - transform.position;
    12.         Quaternion rotation = Quaternion.LookRotation(direction);
    13.       transform.rotation = rotation;
    14.     }
    15.  
    16.  
    17. }
    18.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    If it's 180 degrees wrong then why not flip the "direction" 180 degrees?
     
  3. rennster200

    rennster200

    Joined:
    Jun 21, 2019
    Posts:
    65
    I do actually flip the text 180 degrees but whenever I start the scene, text automatically flips back.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    I didn't say anything about flipping text. You're showing code above, I said flip the direction in reference to your "direction" code i.e. "-direction" or just do the position subtraction the opposite way.
     
    rennster200 likes this.
  5. rennster200

    rennster200

    Joined:
    Jun 21, 2019
    Posts:
    65
    Thank you!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookAt : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     private void Update()
    9.     {
    10.  
    11.         Vector3 direction = transform.position-target.position;
    12.         Quaternion rotation = Quaternion.LookRotation(direction);
    13.       transform.rotation = rotation;
    14.     }
    15.  
    16.  
    17. }
    18.  
    Here is final code if anyone needs it.
     
    MelvMay likes this.