Global Game Jam 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.4 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. using NaughtyAttributes;
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class HandController : MonoBehaviour
  10. {
  11. #region Inspector Fields
  12. [SerializeField, BoxGroup("References")]
  13. private Transform m_handTransform;
  14. [SerializeField, BoxGroup("References")]
  15. private OarController m_oar;
  16. [SerializeField,BoxGroup("settings")]
  17. private Transform m_otherHand;
  18. [SerializeField, BoxGroup("Settings")]
  19. private Vector3 m_axisNormal = Vector3.forward;
  20. [SerializeField, BoxGroup("Settings")]
  21. private Vector3 m_axisForward = Vector3.up;
  22. [SerializeField, BoxGroup("Settings")]
  23. private float m_armRange = 1;
  24. [SerializeField, BoxGroup("Settings"), Range(0.0f, 1.0f)]
  25. private float m_armSpeed = 0.5f;
  26. [SerializeField, BoxGroup("Settings")]
  27. private Vector2 m_startInput = new Vector2(0, 1);
  28. [SerializeField]
  29. private Vector3 m_IKOffset = new Vector3(0, 0, 0.05f);
  30. [SerializeField]
  31. private Quaternion m_IKRotationOffset;
  32. [SerializeField, BoxGroup("Input")]
  33. private InputActionProperty m_inputAxis;
  34. [SerializeField, BoxGroup("Input")]
  35. private InputActionProperty m_row;
  36. #endregion Inspector Fields
  37. #region Private Fields
  38. //Last input of player
  39. public Vector2 m_desiredInput;
  40. private Vector3 m_startPosition;
  41. private Vector3 m_lastPosition;
  42. private bool m_doUndo;
  43. #endregion Private Fields
  44. #region Getters
  45. public new Transform transform => m_handTransform;
  46. #endregion Getters
  47. #region MonoBehaviour Functions
  48. private void Start()
  49. {
  50. m_startPosition = transform.localPosition;
  51. UpdateHand(m_startInput);
  52. }
  53. /// <summary>
  54. /// OnEnable is called when the object becomes enabled and active.
  55. /// </summary>
  56. private void OnEnable()
  57. {
  58. m_inputAxis.action.performed += OnInputRecieved;
  59. m_inputAxis.action.canceled += OnInputRecieved;
  60. m_inputAxis.action.Enable();
  61. m_row.action?.Enable();
  62. }
  63. /// <summary>
  64. /// OnDisable is called when the behaviour becomes disabled.
  65. /// </summary>
  66. private void OnDisable()
  67. {
  68. m_inputAxis.action.performed -= OnInputRecieved;
  69. m_inputAxis.action.canceled -= OnInputRecieved;
  70. m_inputAxis.action.Disable();
  71. m_row.action?.Disable();
  72. }
  73. /// <summary>
  74. /// Update is called once per frame
  75. /// </summary>
  76. private void FixedUpdate()
  77. {
  78. //UpdateHand(m_desiredInput);
  79. }
  80. public Vector3 GetIKPosition()
  81. {
  82. return transform.position + transform.TransformDirection(m_IKOffset);
  83. }
  84. public Quaternion GetIKRotation()
  85. {
  86. return m_oar.transform.rotation * m_IKRotationOffset;
  87. }
  88. #endregion MonoBehaviour Functions
  89. #region Class Functionality
  90. /// <summary>
  91. /// Called every fixed update to move the arms
  92. /// </summary>
  93. /// <param name="input"></param>
  94. public void UpdateHand(Vector2 input, bool registerUndo = true)
  95. {
  96. m_lastPosition = transform.localPosition;
  97. Quaternion rotation = Quaternion.LookRotation(m_axisForward.normalized, m_axisNormal.normalized);
  98. Vector3 axisInput = rotation * new Vector3(input.x, 0, input.y);
  99. Vector3 desiredPosition = m_startPosition + axisInput * m_armRange;
  100. transform.localPosition = Vector3.Lerp(transform.localPosition, desiredPosition, m_armSpeed);
  101. if (m_row.action != null)
  102. {
  103. if (m_row.action.IsPressed())
  104. {
  105. transform.localPosition = Vector3.Lerp(transform.localPosition, m_otherHand.localPosition, 0.5f);
  106. }
  107. }
  108. }
  109. public void UndoLastMovement()
  110. {
  111. Vector3 direction = m_lastPosition - transform.localPosition;
  112. transform.localPosition = m_lastPosition;
  113. }
  114. /// <summary>
  115. /// Called by the input system when the input is changed
  116. /// </summary>
  117. /// <param name="args"></param>
  118. private void OnInputRecieved(InputAction.CallbackContext args)
  119. {
  120. m_desiredInput = args.ReadValue<Vector2>();
  121. }
  122. #endregion Class Functionality
  123. #region Editor Functions
  124. /// <summary>
  125. /// Called when the Component is created or Reset from the Inspector
  126. /// </summary>
  127. private void Reset()
  128. {
  129. m_handTransform = base.transform;
  130. }
  131. #endregion Editor Functions
  132. }