|
|
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using NaughtyAttributes;
-
-
- /// <summary>
- /// Script to control player input and oars
- /// </summary>
- public class OarController : MonoBehaviour
- {
-
- #region Inspector Fields
-
- [SerializeField,BoxGroup("References")]
- private HandController m_leftHand;
-
- [SerializeField, BoxGroup("References")]
- private HandController m_rightHand;
-
- [SerializeField, BoxGroup("References")]
- private Rigidbody m_oarRigidbody;
-
- [SerializeField, BoxGroup("References")]
- private Transform m_body;
-
- [SerializeField]
- private List<Collider> m_forbiddenColliders;
-
- #endregion Inspector Fields
-
- #region Private Fields
-
- float m_distanceFromRightHand;
- bool undoDoneThisFrame = false;
-
- #endregion Private Fields
-
- #region Getters
-
- public new Transform transform => m_oarRigidbody.transform;
-
-
- #endregion Getters
-
-
-
- #region MonoBehaviour Functions
-
- private void Awake()
- {
- m_distanceFromRightHand = Vector3.Distance(m_rightHand.transform.position, transform.position);
- m_oarRigidbody = GetComponent<Rigidbody>();
- }
-
- /// <summary>
- /// OnEnable is called when the object becomes enabled and active.
- /// </summary>
- private void OnEnable()
- {
-
- }
-
- /// <summary>
- /// OnDisable is called when the behaviour becomes disabled.
- /// </summary>
- private void OnDisable()
- {
-
- }
-
- /// <summary>
- /// Update is called once per frame
- /// </summary>
- private void FixedUpdate()
- {
- undoDoneThisFrame = false;
- UpdateTransform();
- }
-
- #endregion MonoBehaviour Functions
-
- #region Class Functionality
-
- private void UpdateTransform()
- {
-
- Vector3 direction =(m_rightHand.transform.position - m_leftHand.transform.position).normalized;
- Vector3 forward = Vector3.Cross(direction, m_body.forward);
- Quaternion rotation = Quaternion.LookRotation(forward, direction);
-
- m_oarRigidbody.MovePosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
- m_oarRigidbody.MoveRotation(rotation);
-
-
- }
-
-
- void OnCollisionEnter(Collision collision)
- {
-
- if (m_forbiddenColliders.Contains(collision.collider) && !undoDoneThisFrame)
- {
- Debug.Log($"Forbidden Collision: {collision.collider.gameObject}");
- undoDoneThisFrame= true;
- m_leftHand.UndoLastMovement();
- m_rightHand.UndoLastMovement();
- }
- else
- {
- Debug.Log($"Ignored Collision: {collision.collider.gameObject}");
- }
-
-
-
- }
-
- #endregion Class Functionality
-
- #region Editor Functions
- /// <summary>
- /// Called when the Component is created or Reset from the Inspector
- /// </summary>
- private void Reset()
- {
- m_oarRigidbody = GetComponentInChildren<Rigidbody>();
- }
- #endregion Editor Functions
-
- }
|