Updated some stuff as I've noticed issues with a few things as I've

written the blog.
This commit is contained in:
2023-04-23 18:55:55 -07:00
parent 2373aec5c4
commit b080364f71
6 changed files with 37 additions and 336 deletions
+20 -14
View File
@@ -1,4 +1,3 @@
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.InputSystem;
@@ -15,12 +14,14 @@ public class CarLogic : MonoBehaviour
public float maxAcceleration = 100;
public float brakingAmount;
public float wheelAccelerationFactor = 6.0f;
private Rigidbody2D _rigidbody;
private Transform _transform;
private float _currentAcceleration;
private float _wheelAccelerationAmount;
private int _wheelOffroadCount = 0;
private int _wheelOnRoadCount = 0;
private float _maxAcceleration;
private float _wheelAccelerationOffset;
public float CurrentAccelerationRatio()
{
@@ -31,9 +32,11 @@ public class CarLogic : MonoBehaviour
{
_rigidbody = GetComponent<Rigidbody2D>();
_transform = transform;
_maxAcceleration = maxAcceleration;
_wheelAccelerationOffset = wheelAccelerationFactor - 4;
}
private void Start()
private void OnEnable()
{
steeringAction.Enable();
accelerationAction.Enable();
@@ -46,18 +49,26 @@ public class CarLogic : MonoBehaviour
wheel.wheelOnRoad = WheelOnRoad;
}
_wheelAccelerationAmount = maxAcceleration / 5f;
_wheelOffroadCount = 4;
_wheelOnRoadCount = 0;
}
private void OnDisable()
{
steeringAction.Disable();
accelerationAction.Disable();
brakeAction.Disable();
}
private void WheelOffRoad()
{
_wheelOffroadCount++;
_wheelOnRoadCount--;
_maxAcceleration = ((_wheelOnRoadCount + _wheelAccelerationOffset) / wheelAccelerationFactor) * maxAcceleration;
}
private void WheelOnRoad()
{
_wheelOffroadCount--;
_wheelOnRoadCount++;
_maxAcceleration = ((_wheelOnRoadCount + _wheelAccelerationOffset) / wheelAccelerationFactor) * maxAcceleration;
}
private void Update()
@@ -75,13 +86,8 @@ public class CarLogic : MonoBehaviour
_currentAcceleration -= brakeValue * brakingAmount * Time.deltaTime;
if (_currentAcceleration > maxAcceleration / 3f)
{
_currentAcceleration -= _wheelOffroadCount * _wheelAccelerationAmount * Time.deltaTime;
}
// TODO: reverse logic.
_currentAcceleration = Mathf.Clamp(_currentAcceleration, 0, maxAcceleration);
_currentAcceleration = Mathf.Clamp(_currentAcceleration, 0, _maxAcceleration);
if (_currentAcceleration > 0.01)
{
+2 -2
View File
@@ -17,10 +17,9 @@ public class LapTime : MonoBehaviour
public string GetTimeString()
{
return
$"Sector 1: {_checkPoint1Time:F}\nSector 2: {_checkPoint2Time:F}\nSector 3: {_checkPoint3Time:F}\nLast Lap Time: {_finishTime:F}\nBest Lap: {_bestTime:F}";
$"Sector 1: {_checkPoint1Time:0.000}\nSector 2: {_checkPoint2Time:0.000}\nSector 3: {_checkPoint3Time:0.000}\nLast Lap Time: {_finishTime:0.0000}\nBest Lap: {_bestTime:0.000}";
}
// Start is called before the first frame update
private void Start()
{
finishLine.CarPassed += FinishedLine;
@@ -69,6 +68,7 @@ public class LapTime : MonoBehaviour
}
else
{
_finishTime = float.MaxValue;
ResetTimers();
}
}