Billboard.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // /******************************************************************************
  2. // * File: Billboard.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // *
  6. // ******************************************************************************/
  7. using UnityEngine;
  8. namespace QCHT.Samples.Proximal
  9. {
  10. public class Billboard : MonoBehaviour
  11. {
  12. public Transform Target;
  13. public bool LockY;
  14. #region MonoBehaviour Functions
  15. private void LateUpdate()
  16. {
  17. Target = Target ? Target : Camera.main.transform;
  18. if (ReferenceEquals(Target, null))
  19. return;
  20. if (LockY)
  21. {
  22. var upVector = Vector3.up;
  23. var wPos = Vector3.Normalize(transform.position - Target.transform.position);
  24. var euler = Quaternion.LookRotation(wPos, upVector).eulerAngles;
  25. var rot = Quaternion.Euler(0, euler.y, 0);
  26. transform.rotation = rot;
  27. }
  28. else
  29. {
  30. var camRot = Target.transform.rotation;
  31. var upVector = camRot * Vector3.up;
  32. var wPos = transform.position + camRot * Vector3.forward;
  33. transform.LookAt(wPos, upVector);
  34. }
  35. }
  36. #endregion
  37. }
  38. }