// %BANNER_BEGIN% // --------------------------------------------------------------------- // %COPYRIGHT_BEGIN% // Copyright (c) 2022-2023 Magic Leap, Inc. All Rights Reserved. // Use of this file is governed by the Magic Leap 2 Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2 // Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein. // %COPYRIGHT_END% // --------------------------------------------------------------------- // %BANNER_END% using UnityEngine; namespace MagicLeap.Spectator { public class LazyBillboard : MonoBehaviour { #region Public variables [Tooltip("The forwards distance from the camera that this object should be placed.")] public float CanvasDistanceForwards = 1.5f; [Tooltip("The upwards distance from the camera that this object should be placed.")] public float CanvasDistanceUpwards = 0.0f; [Tooltip("The right distance from the camera that this object should be placed.")] public float CanvasDistanceRight = 0.0f; [Tooltip("The speed at which this object changes its position.")] public float PositionLerpSpeed = 5f; [Tooltip("The speed at which this object changes its rotation.")] public float RotationLerpSpeed = 5f; #endregion #region Private varibles // The camera this object will be in front of private Camera _camera; #endregion #region MonoBehaviour methods void Awake() { _camera = Camera.main; } private void OnEnable() { transform.position = _camera.transform.position + (_camera.transform.forward * CanvasDistanceForwards) + (_camera.transform.up * CanvasDistanceUpwards) + (_camera.transform.right * CanvasDistanceRight); } void Update() { // Move the object CanvasDistance units in front of the camera. float posSpeed = Time.deltaTime * PositionLerpSpeed; Vector3 posTo = _camera.transform.position + (_camera.transform.forward * CanvasDistanceForwards) + (_camera.transform.up * CanvasDistanceUpwards) + (_camera.transform.right * CanvasDistanceRight); transform.position = Vector3.SlerpUnclamped(transform.position, posTo, posSpeed); // Rotate the object to face the camera. float rotSpeed = Time.deltaTime * RotationLerpSpeed; Quaternion rotTo = Quaternion.LookRotation(transform.position - _camera.transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotTo, rotSpeed); } #endregion } }