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.

155 lines
3.7 KiB

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]
  15. private Transform m_otherHand;
  16. [SerializeField, BoxGroup("Settings")]
  17. private Vector3 m_axisNormal = Vector3.forward;
  18. [SerializeField, BoxGroup("Settings")]
  19. private Vector3 m_axisForward = Vector3.up;
  20. [SerializeField, BoxGroup("Settings")]
  21. private float m_armRange = 1;
  22. [SerializeField, BoxGroup("Settings"),Range(0.0f,1.0f)]
  23. private float m_armSpeed = 0.5f;
  24. [SerializeField, BoxGroup("Input")]
  25. private InputActionProperty m_inputAxis;
  26. [SerializeField, BoxGroup("Input")]
  27. private InputActionProperty m_row;
  28. #endregion Inspector Fields
  29. #region Private Fields
  30. //Last input of player
  31. private Vector2 m_desiredInput;
  32. private Vector3 m_startPosition;
  33. private Vector3 m_lastPosition;
  34. #endregion Private Fields
  35. #region Getters
  36. public new Transform transform => m_handTransform;
  37. #endregion Getters
  38. #region MonoBehaviour Functions
  39. private void Start()
  40. {
  41. m_startPosition = transform.localPosition;
  42. }
  43. /// <summary>
  44. /// OnEnable is called when the object becomes enabled and active.
  45. /// </summary>
  46. private void OnEnable()
  47. {
  48. m_inputAxis.action.performed += OnInputRecieved;
  49. m_inputAxis.action.canceled += OnInputRecieved;
  50. m_inputAxis.action.Enable();
  51. m_row.action?.Enable();
  52. }
  53. /// <summary>
  54. /// OnDisable is called when the behaviour becomes disabled.
  55. /// </summary>
  56. private void OnDisable()
  57. {
  58. m_inputAxis.action.performed -= OnInputRecieved;
  59. m_inputAxis.action.canceled -= OnInputRecieved;
  60. m_inputAxis.action.Disable();
  61. m_row.action?.Disable();
  62. }
  63. /// <summary>
  64. /// Update is called once per frame
  65. /// </summary>
  66. private void FixedUpdate()
  67. {
  68. UpdateHand(m_desiredInput);
  69. }
  70. #endregion MonoBehaviour Functions
  71. #region Class Functionality
  72. /// <summary>
  73. /// Called every fixed update to move the arms
  74. /// </summary>
  75. /// <param name="input"></param>
  76. private void UpdateHand(Vector2 input)
  77. {
  78. m_lastPosition = transform.localPosition;
  79. Quaternion rotation = Quaternion.LookRotation(m_axisForward.normalized, m_axisNormal.normalized);
  80. Vector3 axisInput = transform.rotation * rotation * new Vector3(input.x, 0, input.y);
  81. Vector3 desiredPosition = m_startPosition + axisInput * m_armRange;
  82. transform.localPosition = Vector3.Lerp(transform.localPosition, desiredPosition, m_armSpeed);
  83. if (m_row.action != null)
  84. {
  85. if (m_row.action.IsPressed())
  86. {
  87. transform.localPosition = Vector3.Lerp(transform.localPosition, m_otherHand.localPosition, 0.5f);
  88. }
  89. }
  90. }
  91. public void UndoLastMovement()
  92. {
  93. transform.localPosition = m_lastPosition;
  94. }
  95. /// <summary>
  96. /// Called by the input system when the input is changed
  97. /// </summary>
  98. /// <param name="args"></param>
  99. private void OnInputRecieved(InputAction.CallbackContext args)
  100. {
  101. m_desiredInput = args.ReadValue<Vector2>();
  102. }
  103. #endregion Class Functionality
  104. #region Editor Functions
  105. /// <summary>
  106. /// Called when the Component is created or Reset from the Inspector
  107. /// </summary>
  108. private void Reset()
  109. {
  110. m_handTransform = base.transform;
  111. }
  112. #endregion Editor Functions
  113. }