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

Resolved How to make a 3D TMP text always face to the camera

Discussion in 'AR' started by TropicalCyborg, Mar 29, 2022.

  1. TropicalCyborg

    TropicalCyborg

    Joined:
    Mar 19, 2014
    Posts:
    28
    Hi, I am building an experience that instantiates several objects on different locations and when you come close enough of one of those objects it displays a text with some information. I am using the LookAt method to try to make the text point to the ARSession origin. I've already tried to make them point to the AR camera too. The problem is that the text 3D which are parented to the instantiated object always displays facing on the opposite direction. I've already tried to invert the target orientation and also rotate the text objects inside the parent prefab with no success. Can anybody help me?

    Code (CSharp):
    1. public class OrientPinText : MonoBehaviour
    2. {
    3.  
    4.     private Transform arSessionOrigin;
    5.     private Transform textTransform;
    6.  
    7.     private void OnEnable() {
    8.         arSessionOrigin = GameObject.Find("AR Session Origin").GetComponent<Transform>();
    9.         textTransform = gameObject.GetComponent<Transform>();
    10.        
    11.        
    12.     }
    13.     void LateUpdate()
    14.     {
    15.         // Vector3 textRotation = textTransform.localEulerAngles;
    16.         // textTransform.InverseTransformVector(textRotation);
    17.         GameObject target = new GameObject();
    18.         target.transform.position = arSessionOrigin.transform.position;
    19.         // target.transform.rotation = arSessionOrigin.rotation;
    20.         target.transform.rotation = Quaternion.Inverse(arSessionOrigin.rotation);
    21.         textTransform.LookAt(target.transform, Vector3.up);
    22.     }
    23. }
     
  2. FernwehSmith

    FernwehSmith

    Joined:
    Jan 31, 2022
    Posts:
    36
    Hey so if I understand correctly you're trying to get the text to always face the camera? If so then the 'LookAt' method seems to be the way to go, you just need to then use 'RotateAround' immediately after to rotate the text 180 degrees around its local up axis. They way I've got my scene setup is text parented to canvas parented to object. The following script is placed on the canvas object. It works for me when moving the camera, session origin and object itself. Also just make sure that the AR Camera is marked with the 'MainCamera' tag, otherwise it won't find the right camera. Hope this helps and that I've understood your problem correctly :)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. //Place on Canvas Object
    4. // Text -> Canvas (this) -> Model Object (-> =  is parented to...)
    5. public class OrientToCamera : MonoBehaviour
    6. {
    7.     private Transform mainCam;
    8.  
    9.     private void OnEnable() {
    10.         mainCam = Camera.main.transform;
    11.         Debug.Log("Main Cam = " + mainCam.name);
    12.     }
    13.  
    14.     private void LateUpdate() {
    15.         transform.LookAt(mainCam);
    16.         transform.RotateAround(transform.position, transform.up, 180f);
    17.     }
    18. }
     
    TropicalCyborg and todds_unity like this.
  3. TropicalCyborg

    TropicalCyborg

    Joined:
    Mar 19, 2014
    Posts:
    28
    Great, Bro! It helped! Thanks a lot!
     
    FernwehSmith likes this.