Billboard.cs 902 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Ximmerse.XR
  5. {
  6. public class Billboard : MonoBehaviour
  7. {
  8. Transform mainCamera;
  9. public bool revert;
  10. public bool keepHorizontal = true;
  11. // Update is called once per frame
  12. void LateUpdate()
  13. {
  14. if (!mainCamera)
  15. {
  16. mainCamera = Camera.main.transform;
  17. }
  18. if (!mainCamera)
  19. {
  20. return;
  21. }
  22. Vector3 direction = (transform.position - mainCamera.position).normalized;
  23. if(keepHorizontal)
  24. {
  25. direction.y = 0;
  26. direction = direction.normalized;
  27. }
  28. if(revert)
  29. {
  30. direction *= -1;
  31. }
  32. transform.forward = direction;
  33. }
  34. }
  35. }