using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using NaughtyAttributes; /// /// /// public class HandController : MonoBehaviour { #region Inspector Fields [SerializeField, BoxGroup("References")] private Transform m_handTransform; [SerializeField, BoxGroup("References")] private OarController m_oar; [SerializeField,BoxGroup("settings")] private Transform m_otherHand; [SerializeField, BoxGroup("Settings")] private Vector3 m_axisNormal = Vector3.forward; [SerializeField, BoxGroup("Settings")] private Vector3 m_axisForward = Vector3.up; [SerializeField, BoxGroup("Settings")] private float m_armRange = 1; [SerializeField, BoxGroup("Settings"), Range(0.0f, 1.0f)] private float m_armSpeed = 0.5f; [SerializeField, BoxGroup("Settings")] private Vector2 m_startInput = new Vector2(0, 1); [SerializeField, BoxGroup("Input")] private InputActionProperty m_inputAxis; [SerializeField, BoxGroup("Input")] private InputActionProperty m_row; #endregion Inspector Fields #region Private Fields //Last input of player private Vector2 m_desiredInput; private Vector3 m_startPosition; private Vector3 m_lastPosition; private bool m_doUndo; #endregion Private Fields #region Getters public new Transform transform => m_handTransform; #endregion Getters #region MonoBehaviour Functions private void Start() { m_startPosition = transform.localPosition; UpdateHand(m_startInput); } /// /// OnEnable is called when the object becomes enabled and active. /// private void OnEnable() { m_inputAxis.action.performed += OnInputRecieved; m_inputAxis.action.canceled += OnInputRecieved; m_inputAxis.action.Enable(); m_row.action?.Enable(); } /// /// OnDisable is called when the behaviour becomes disabled. /// private void OnDisable() { m_inputAxis.action.performed -= OnInputRecieved; m_inputAxis.action.canceled -= OnInputRecieved; m_inputAxis.action.Disable(); m_row.action?.Disable(); } /// /// Update is called once per frame /// private void FixedUpdate() { UpdateHand(m_desiredInput); } #endregion MonoBehaviour Functions #region Class Functionality /// /// Called every fixed update to move the arms /// /// private void UpdateHand(Vector2 input, bool registerUndo = true) { m_lastPosition = transform.localPosition; Quaternion rotation = Quaternion.LookRotation(m_axisForward.normalized, m_axisNormal.normalized); Vector3 axisInput = transform.rotation * rotation * new Vector3(input.x, 0, input.y); Vector3 desiredPosition = m_startPosition + axisInput * m_armRange; transform.localPosition = Vector3.Lerp(transform.localPosition, desiredPosition, m_armSpeed); if (m_row.action != null) { if (m_row.action.IsPressed()) { transform.localPosition = Vector3.Lerp(transform.localPosition, m_otherHand.localPosition, 0.5f); } } } public void UndoLastMovement() { Vector3 direction = m_lastPosition - transform.localPosition; transform.localPosition += direction; } /// /// Called by the input system when the input is changed /// /// private void OnInputRecieved(InputAction.CallbackContext args) { m_desiredInput = args.ReadValue(); } #endregion Class Functionality #region Editor Functions /// /// Called when the Component is created or Reset from the Inspector /// private void Reset() { m_handTransform = base.transform; } #endregion Editor Functions }