Joshua Reason 1 year ago
parent
commit
c3418b4650
4 changed files with 65 additions and 8 deletions
  1. BIN
      Assets/Scenes/Development/OarVelocityController.unity
  2. +7
    -0
      Assets/Scenes/Development/OarVelocityController.unity.meta
  3. +12
    -0
      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:

+ 12
- 0
Assets/Scripts/BoatController.cs View File

@ -68,12 +68,24 @@ public class BoatController : MonoBehaviour
rigidBody.AddTorque(transform.up * -RotationFactor);
}
public void RowLeft(float velocity)
{
rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration);
rigidBody.AddTorque(transform.up * -RotationFactor * velocity);
}
public void RowRight()
{
rigidBody.AddForce(transform.forward * ForwardFactor, ForceMode.Acceleration);
rigidBody.AddTorque(transform.up * RotationFactor);
}
public void RowRight(float velocity)
{
rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration);
rigidBody.AddTorque(transform.up * RotationFactor * velocity);
}
public void Brake()
{
rigidBody.AddForce(rigidBody.velocity * - BrakeFactor);

+ 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