CameraToLook.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using UnityEngine;
  3. /* 弓的摄像机总会LookAt该节点的Point子节点
  4. 九轴数据是直接应用到该节点的(特意分离而不直接应用到弓的摄像机) 是为了避免z轴旋转
  5. */
  6. public class CameraToLook : MonoBehaviour
  7. {
  8. [NonSerialized] public Transform point;
  9. [SerializeField] bool onlyParseRotation = false;
  10. public Action<Quaternion> onParseRotation;
  11. public static CameraToLook ins;
  12. void Awake()
  13. {
  14. ins = this;
  15. point = transform.Find("Point");
  16. }
  17. void OnDestroy()
  18. {
  19. if (ins == this) ins = null;
  20. }
  21. public Quaternion localRotation {
  22. get
  23. {
  24. return transform.localRotation;
  25. }
  26. set
  27. {
  28. transform.localRotation = value;
  29. if (onlyParseRotation) {
  30. //去掉z轴影响
  31. Vector3 normalVector = value * Vector3.forward;
  32. Quaternion normalQuat = Quaternion.LookRotation(normalVector);
  33. try
  34. {
  35. if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  36. onParseRotation?.Invoke(normalQuat);
  37. #if UNITY_EDITOR
  38. else if (InfraredDemo.DebugInEditor) onParseRotation?.Invoke(normalQuat);
  39. #endif
  40. }
  41. catch (System.Exception e)
  42. {
  43. Debug.LogError(e);
  44. }
  45. }
  46. }
  47. }
  48. }