-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillboard.cs
More file actions
27 lines (24 loc) · 803 Bytes
/
Billboard.cs
File metadata and controls
27 lines (24 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using UnityEngine;
/// <summary>
/// Rotates the object towards the camera.
/// How to use: Add the MonoBehaviour to the UI element that needs to face the camera.
/// </summary>
public class Billboard : MonoBehaviour
{
[Tooltip("The UI element will face this Camera. Defaults to the Main Camera if left empty")]
public Camera MyCamera;
void Start()
{
if(MyCamera == null)
{
Debug.Log("No Camera was selected for " + gameObject.name + " setting Main Camera");
MyCamera = Camera.main;
}
}
void Update()
{
var transform = GetComponent<Transform>();
transform.LookAt(transform.position + MyCamera.transform.rotation * Vector3.forward,
MyCamera.transform.rotation * Vector3.up);
}
}