Browse Source

Velocity oar control

main
Joshua 1 year ago
parent
commit
163e90815b
4 changed files with 54 additions and 9 deletions
  1. BIN
      Assets/Scenes/Development/OarVelocityController.unity
  2. +7
    -0
      Assets/Scenes/Development/OarVelocityController.unity.meta
  3. +1
    -1
      Assets/Scripts/BoatController.cs
  4. +43
    -8
      Assets/Scripts/Player/BoatRowController.cs

BIN
Assets/Scenes/Development/OarVelocityController.unity (Stored with Git LFS) View File

size 89254

+ 7
- 0
Assets/Scenes/Development/OarVelocityController.unity.meta View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: e131d8dbbf4fd8243bfe76bc3d92edeb
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 1
- 1
Assets/Scripts/BoatController.cs View File

@ -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()

+ 43
- 8
Assets/Scripts/Player/BoatRowController.cs View File

@ -13,11 +13,15 @@ public class BoatRowController : MonoBehaviour
#region Inspector Fields
public Transform m_oarTip;
public UnityEvent m_OnRow;
public UnityEvent<float> m_OnRow;
public UnityEvent m_OnBrake;
public int velocityOverFrames = 8;
#endregion Inspector Fields
#region Private Fields
private Vector3 m_lastKnownOarPosition;
private Queue<float> m_velocityQueue = new Queue<float>();
#endregion Private Fields
#region Getters
@ -60,7 +64,8 @@ public class BoatRowController : MonoBehaviour
OarController oar = other.GetComponentInParent<OarController>();
if (oar != null)
{
m_lastKnownOarPosition = m_oarTip.localPosition;
m_lastKnownOarPosition = transform.InverseTransformPoint(m_oarTip.position);
m_velocityQueue = new Queue<float>();
}
}
@ -69,20 +74,50 @@ public class BoatRowController : MonoBehaviour
OarController oar = other.GetComponentInParent<OarController>();
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
{

Loading…
Cancel
Save