From 163e90815b36b1b549f57695928e9ff65d4cfe4a Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 5 Feb 2023 15:03:53 +1100 Subject: [PATCH] Velocity oar control --- .../Development/OarVelocityController.unity | 3 ++ .../OarVelocityController.unity.meta | 7 +++ Assets/Scripts/BoatController.cs | 2 +- Assets/Scripts/Player/BoatRowController.cs | 51 ++++++++++++++++--- 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 Assets/Scenes/Development/OarVelocityController.unity create mode 100644 Assets/Scenes/Development/OarVelocityController.unity.meta diff --git a/Assets/Scenes/Development/OarVelocityController.unity b/Assets/Scenes/Development/OarVelocityController.unity new file mode 100644 index 0000000..5356253 --- /dev/null +++ b/Assets/Scenes/Development/OarVelocityController.unity @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:63557b82d59b7a99c99441c6c75f27c833f8c15110956708728a58243f1c60ed +size 89254 diff --git a/Assets/Scenes/Development/OarVelocityController.unity.meta b/Assets/Scenes/Development/OarVelocityController.unity.meta new file mode 100644 index 0000000..7784a54 --- /dev/null +++ b/Assets/Scenes/Development/OarVelocityController.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e131d8dbbf4fd8243bfe76bc3d92edeb +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BoatController.cs b/Assets/Scripts/BoatController.cs index b2681bf..6c55434 100644 --- a/Assets/Scripts/BoatController.cs +++ b/Assets/Scripts/BoatController.cs @@ -83,7 +83,7 @@ public class BoatController : MonoBehaviour public void RowRight(float velocity) { rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration); - rigidBody.AddTorque(transform.up * -RotationFactor * velocity); + rigidBody.AddTorque(transform.up * RotationFactor * velocity); } public void Brake() diff --git a/Assets/Scripts/Player/BoatRowController.cs b/Assets/Scripts/Player/BoatRowController.cs index b496617..7d3771f 100644 --- a/Assets/Scripts/Player/BoatRowController.cs +++ b/Assets/Scripts/Player/BoatRowController.cs @@ -13,11 +13,15 @@ public class BoatRowController : MonoBehaviour #region Inspector Fields public Transform m_oarTip; - public UnityEvent m_OnRow; + public UnityEvent m_OnRow; + public UnityEvent m_OnBrake; + public int velocityOverFrames = 8; #endregion Inspector Fields #region Private Fields private Vector3 m_lastKnownOarPosition; + + private Queue m_velocityQueue = new Queue(); #endregion Private Fields #region Getters @@ -60,7 +64,8 @@ public class BoatRowController : MonoBehaviour OarController oar = other.GetComponentInParent(); if (oar != null) { - m_lastKnownOarPosition = m_oarTip.localPosition; + m_lastKnownOarPosition = transform.InverseTransformPoint(m_oarTip.position); + m_velocityQueue = new Queue(); } } @@ -69,20 +74,50 @@ public class BoatRowController : MonoBehaviour OarController oar = other.GetComponentInParent(); if(oar != null) { - Vector3 direction = m_lastKnownOarPosition - m_oarTip.localPosition; + Vector3 localOarTipPosition = transform.InverseTransformPoint(m_oarTip.position); + + Vector3 direction = m_lastKnownOarPosition - localOarTipPosition; float directionality = Vector3.Dot(direction,Vector3.forward); - if (directionality > 0) + m_velocityQueue.Enqueue(directionality * Time.deltaTime); + + if(m_velocityQueue.Count > velocityOverFrames) + { + m_velocityQueue.Dequeue(); + } + + float averageVelocity = 0; + if (m_velocityQueue.Count > 0) + { + var v_arr = m_velocityQueue.ToArray(); + + for(int i = 0; i < v_arr.Length; i++) + { + averageVelocity += v_arr[i]; + } + averageVelocity /= v_arr.Length; + } + + if (averageVelocity > 0) { - Debug.Log($"Row! ({directionality})"); - m_OnRow.Invoke(); + if(averageVelocity < 0.000001) + { + m_OnBrake.Invoke(); + Debug.Log($"Brake! ({averageVelocity})"); + } + else + { + Debug.Log($"Row! ({averageVelocity})"); + m_OnRow.Invoke(averageVelocity); + } } else { - Debug.Log($"No Row: {directionality}"); + Debug.Log($"No Row: {averageVelocity}"); + m_OnBrake.Invoke(); } - m_lastKnownOarPosition=m_oarTip.localPosition; + m_lastKnownOarPosition = localOarTipPosition; } else {