crazy car physics

This commit is contained in:
2024-04-05 14:06:07 +02:00
parent ce41f0141b
commit b277104435
5 changed files with 4073 additions and 157 deletions

View File

@@ -38,7 +38,7 @@ public class carcontrolv2 : MonoBehaviour
// Calculate how close the car is to top speed
// as a number from zero to one
float speedFactor = Mathf.InverseLerp(0, maxSpeed, forwardSpeed);
float speedFactor = Mathf.InverseLerp(0, maxSpeed / 4, forwardSpeed);
// Use that to calculate how much torque is available
// (zero torque at top speed)
@@ -46,6 +46,7 @@ public class carcontrolv2 : MonoBehaviour
// …and to calculate how much to steer
// (the car steers more gently at top speed)
float currentSteerRange = Mathf.Lerp(steeringRange, steeringRangeAtMaxSpeed, speedFactor);
// Check whether the user input is in the same direction
@@ -56,6 +57,11 @@ public class carcontrolv2 : MonoBehaviour
bool isBraking = (vInput < 0 && forwardSpeed > 0) || (vInput > 0 && forwardSpeed < 0);
if (vInput > 0 && forwardSpeed < 0)
{
isAccelerating = false;
}
foreach (var wheel in wheels)
{
// Apply steering to Wheel colliders that have "Steerable" enabled
@@ -63,6 +69,12 @@ public class carcontrolv2 : MonoBehaviour
{
wheel.WheelCollider.steerAngle = hInput * currentSteerRange;
}
if (isBraking)
{
wheel.WheelCollider.brakeTorque = Mathf.Abs(vInput) * brakeTorque;
//wheel.WheelCollider.motorTorque = 0;
}
if (isAccelerating)
{
@@ -74,11 +86,7 @@ public class carcontrolv2 : MonoBehaviour
wheel.WheelCollider.brakeTorque = 0;
}
if (isBraking)
{
wheel.WheelCollider.brakeTorque = Mathf.Abs(vInput) * brakeTorque;
//wheel.WheelCollider.motorTorque = 0;
}
if (isStopping)
{