FollowMouseMove.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using UnityEngine;
  12. /// <summary> A follow mouse move. </summary>
  13. public class FollowMouseMove : MonoBehaviour
  14. {
  15. #if UNITY_EDITOR || UNITY_STANDALONE_WIN
  16. /// <summary> The camera tran. </summary>
  17. [SerializeField] Transform m_CameraTran;
  18. /// <summary> The move speed. </summary>
  19. [SerializeField] float m_MoveSpeed = 5.0f;
  20. /// <summary> Gets the mouse y coordinate. </summary>
  21. /// <value> The m mouse y coordinate. </value>
  22. private float m_MouseX, m_MouseY;
  23. /// <summary> True to need update rotation. </summary>
  24. private bool m_NeedUpdateRotation = false;
  25. /// <summary> True to need update position. </summary>
  26. private bool m_NeedUpdatePosition = false;
  27. /// <summary> Updates this object. </summary>
  28. void Update()
  29. {
  30. if (Input.GetKeyUp(KeyCode.R))
  31. {
  32. m_CameraTran.localPosition = Vector3.zero;
  33. m_CameraTran.localRotation = Quaternion.Euler(Vector3.zero);
  34. }
  35. if (Input.GetMouseButtonDown(1))
  36. {
  37. m_NeedUpdateRotation = true;
  38. }
  39. if (Input.GetMouseButtonUp(1))
  40. {
  41. m_NeedUpdateRotation = false;
  42. }
  43. if (m_NeedUpdateRotation)
  44. {
  45. m_MouseX = Input.GetAxis("Mouse X") * m_MoveSpeed;
  46. m_MouseY = Input.GetAxis("Mouse Y") * m_MoveSpeed;
  47. m_CameraTran.localEulerAngles = m_CameraTran.localEulerAngles + new Vector3(-m_MouseY, m_MouseX, 0);
  48. }
  49. if (Input.GetMouseButtonDown(0))
  50. {
  51. m_NeedUpdatePosition = true;
  52. }
  53. if (Input.GetMouseButtonUp(0))
  54. {
  55. m_NeedUpdatePosition = false;
  56. }
  57. if (m_NeedUpdatePosition)
  58. {
  59. m_MouseX = Input.GetAxis("Mouse X");
  60. m_MouseY = Input.GetAxis("Mouse Y");
  61. m_CameraTran.localPosition = m_CameraTran.localPosition + m_CameraTran.forward * m_MouseY + m_CameraTran.right * m_MouseX;
  62. }
  63. }
  64. #endif
  65. }
  66. }