You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
819 B
C#

3 years ago
using UnityEngine;
namespace MalbersAnimations.Utilities
{
/// <summary>Used to align the UI to the Camera Direction</summary>
[AddComponentMenu("Malbers/UI/Look At Camera")]
public class LookAtCamera : MonoBehaviour
{
public bool justY = true;
public Vector3 Offset;
Transform cam;
private void Start()
{
cam = Camera.main.transform;
}
void Update()
{
var lookPos = cam.position - transform.position;
lookPos.y = 0;
if (lookPos != Vector3.zero)
{
var rotation = Quaternion.LookRotation(lookPos);
transform.eulerAngles = (new Vector3(justY ? 0 : rotation.eulerAngles.x, rotation.eulerAngles.y, 0) + Offset);
}
}
}
}