Search Unity

Please help me with script making UI canvas look towards camera.

Discussion in 'Scripting' started by darkhog, Jun 25, 2015.

  1. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    Hello. For editor for my upcoming game, I'm working on object inspector widget where you can edit various properties of currently selected game object. The inspector is in full 3D and "anchored" to currently selected game object (for the inspiration for this I've used Minority Report). The reason for it is that otherwise screen would be too busy (and it looks way cooler).

    However, I've had to make sure the inspector is always facing the camera, so I've developed a script achieving that. First I've decided to use LookAt, however this made the canvas behave in weird ways (such as flipping over at certain angles, looking to be upside-down at other, etc.), so I've replaced it with another, custom thing.

    It works for the most part, however when I look up, it tends to fall over and clip through the camera. I'd like this small issue to be fixed. Could you help me with that?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CanvasTripod : MonoBehaviour {
    6.    public GameObject cam;
    7.    // Use this for initialization
    8.    void Start () {
    9.  
    10.    }
    11.    void Update(){
    12.      //this.transform.LookAt(cam.transform);
    13.      //this.transform.Rotate(0,180,0);
    14.      Quaternion camrot = cam.transform.rotation;
    15.      this.transform.rotation = camrot;
    16.    }
    17.    // Update is called once per frame
    18.    void LateUpdate () {
    19.      if (LevEditGlobals.currentGO!=null){
    20.        this.transform.position = LevEditGlobals.currentGO.transform.position;
    21.      }
    22.      
    23.    }
    24. }
    Also I'm sorry for the barely coherent English sentences, but I'm not native speaker and I have trouble finding words and remember about grammar differences when I'm tired (which I am now).
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This should billboard properly:
    Code (csharp):
    1.  
    2. Camera camera = Camera.main;
    3.  
    4. transform.LookAt(transform.position + camera.transform.rotation * Vector3.back, camera.transform.rotation * Vector3.up);
    5.  
     
  3. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    Thanks, though following line was needed after lookat:
    Code (csharp):
    1. this.transform.Rotate(0,180,0);
    because I've seen back side of the canvas. But overall it's better. Thank you!
     
  4. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    94
    That's because it's using Vector3.back rather than forward...

    Code (CSharp):
    1. public void Update()
    2. {
    3.     Camera camera = Camera.main;
    4.     transform.LookAt(transform.position + camera.transform.rotation * Vector3.forward, camera.transform.rotation * Vector3.up);
    5. }
     
  5. AaronRawlinson

    AaronRawlinson

    Joined:
    Jun 10, 2018
    Posts:
    7
    I've just tried to implement this code into my couch co-op game. I've got it set on items that have to be collected. It works fine when the item hasn't yet been touch by the player. But as soon as the object is moved, the canvas goes crazy and distorted at weird angles.

    For more context, the game is top-down (ish) with 1-4 players. The script is on the world space canvas of each item that can be picked up by the players. As soon as the player picks up an item to move it, all hell breaks loose.

    upload_2020-5-16_12-55-26.png
     
    Last edited: May 17, 2020
  6. villalbaweb

    villalbaweb

    Joined:
    Sep 29, 2019
    Posts:
    4
    Addd a script to your canvas and update the following

    Code (CSharp):
    1.         void Update()
    2.         {
    3.             transform.forward = Camera.main.transform.forward;
    4.         }