FollowCamera.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEngine;
  2. namespace Rokid.UXR.Utility
  3. {
  4. [ExecuteAlways]
  5. public class FollowCamera : MonoBehaviour
  6. {
  7. public enum FollowType
  8. {
  9. RotationAndPosition, //Follows the position and rotation of the camera.
  10. PositionOnly, // Follows only the position of the camera
  11. RotationOnly // Follows only the rotation of the camera
  12. }
  13. [SerializeField, Tooltip("Follow Camera Pose Type")]
  14. private FollowType followType = FollowType.RotationAndPosition;
  15. [SerializeField, Tooltip("Deviation from Camera Position")]
  16. public Vector3 offsetPosition = new Vector3(0, 0, 0);
  17. [SerializeField, Tooltip("Deviation from Camera Rotation")]
  18. private Quaternion offsetRotation = Quaternion.identity;
  19. [SerializeField, Tooltip("Lock X-axis while following camera rotation")]
  20. private bool lockRotX = false;
  21. [SerializeField, Tooltip("Lock Y-axis while following camera rotation")]
  22. private bool lockRotY = false;
  23. [SerializeField, Tooltip("Lock Z-axis while following camera rotation")]
  24. private bool lockRotZ = false;
  25. [SerializeField, Tooltip("adjust camera center by fov")]
  26. private bool adjustCenterByFov = true;
  27. private Vector3 oriOffsetPosition = Vector3.zero;
  28. private void Start()
  29. {
  30. oriOffsetPosition = offsetPosition;
  31. AdjustCenterByCameraByFov(adjustCenterByFov);
  32. }
  33. private void LateUpdate()
  34. {
  35. switch (followType)
  36. {
  37. case FollowType.RotationAndPosition:
  38. this.transform.position = MainCameraCache.mainCamera.transform.TransformPoint(offsetPosition);
  39. Vector3 cameraEuler = (offsetRotation * MainCameraCache.mainCamera.transform.rotation).eulerAngles;
  40. this.transform.rotation = Quaternion.Euler(lockRotX ? 0 : cameraEuler.x, lockRotY ? 0 : cameraEuler.y, lockRotZ ? 0 : cameraEuler.z);
  41. break;
  42. case FollowType.PositionOnly:
  43. this.transform.position = MainCameraCache.mainCamera.transform.position + offsetPosition;
  44. break;
  45. case FollowType.RotationOnly:
  46. Vector3 cameraEuler1 = (offsetRotation * MainCameraCache.mainCamera.transform.rotation).eulerAngles;
  47. this.transform.rotation = Quaternion.Euler(lockRotX ? 0 : cameraEuler1.x, lockRotY ? 0 : cameraEuler1.y, lockRotZ ? 0 : cameraEuler1.z);
  48. break;
  49. }
  50. }
  51. public void AdjustCenterByCameraByFov(bool adjustCenterByFov, bool useLeftEyeFov = true)
  52. {
  53. this.adjustCenterByFov = adjustCenterByFov;
  54. if (adjustCenterByFov)
  55. {
  56. Vector3 center = Utils.GetCameraCenter(oriOffsetPosition.z);
  57. offsetPosition = center + new Vector3(oriOffsetPosition.x, oriOffsetPosition.y, 0);
  58. RKLog.KeyInfo($"====FollowCamera==== {this.gameObject.name}, offsetPosition:{offsetPosition},oriOffsetPosition:{oriOffsetPosition},center:{center}");
  59. }
  60. else
  61. {
  62. offsetPosition = oriOffsetPosition;
  63. RKLog.KeyInfo($"====FollowCamera==== {this.gameObject.name} offsetPosition:{offsetPosition}");
  64. }
  65. }
  66. }
  67. }