CameraToLook.cs 1.6 KB

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