Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How to make the ui always align the camera?

Discussion in 'UGUI & TextMesh Pro' started by manlaikin1994, May 19, 2015.

  1. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    I want to the following UI.
    The ui is always align to camera, how to do it?

    i try sth like create ui in world space and rotation = camera rotation.
    It not look same.
    Plz help or tell me the key words to let me search.
    I really dk the key words...
    i will be glad if someone try to help me
     

    Attached Files:

  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Easiest way is to set it to screen space.

    For world space canvases simply call transform.LookAt(Camera.main.transform) in Update or LateUpdate.

    Edit: Or simply do transfom.rotation = Camera.main.transform.rotation. Probably more performant.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    would the edited in line make the ui face away from the camera? I think you'd need the inverse. apparently not

    Code (csharp):
    1.  
    2. public class RotateTowardsCamera : MonoBehaviour
    3. {
    4.     public Camera target;
    5.  
    6.     void Start ()
    7.     {
    8.        if(!target)
    9.         {
    10.             target = Camera.main;
    11.         }
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         Quaternion cameraRotation = target.transform.rotation;
    17. // optionally ignore all but the y rotation, if you want it to be "square on" to the camera comment out the next line
    18.         cameraRotation = Quaternion.Euler(0, cameraRotation.eulerAngles.y, 0);
    19.         transform.rotation = cameraRotation;
    20.     }
    21. }
    22.  
    :)
     
    Last edited: May 19, 2015