Kinda lost the iteration aspect of this and just finished this prototype
up. So iteration 2 technically. Cleaned up some CarLogic code, did todos notably the brake force. Added the concept of wheels being on or off road and hampering acceleration if off road. Decided to do some camera work to get a better feel for acceleration/speed. Implemented Lap Timers, sections, and validation upon finishing a lap.
This commit is contained in:
+39
-15
@@ -1,3 +1,4 @@
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
@@ -13,11 +14,19 @@ public class CarLogic : MonoBehaviour
|
||||
public float steeringAnglePerSecond = 10;
|
||||
|
||||
public float maxAcceleration = 100;
|
||||
public float brakingAmount;
|
||||
|
||||
private Rigidbody2D _rigidbody;
|
||||
private Transform _transform;
|
||||
private float _currentAcceleration;
|
||||
private float _wheelAccelerationAmount;
|
||||
private int _wheelOffroadCount = 0;
|
||||
|
||||
public float CurrentAccelerationRatio()
|
||||
{
|
||||
return _currentAcceleration / maxAcceleration;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rigidbody = GetComponent<Rigidbody2D>();
|
||||
@@ -29,6 +38,26 @@ public class CarLogic : MonoBehaviour
|
||||
steeringAction.Enable();
|
||||
accelerationAction.Enable();
|
||||
brakeAction.Enable();
|
||||
|
||||
var wheels = GetComponentsInChildren<Wheel>();
|
||||
foreach (var wheel in wheels)
|
||||
{
|
||||
wheel.wheelOffRoad = WheelOffRoad;
|
||||
wheel.wheelOnRoad = WheelOnRoad;
|
||||
}
|
||||
|
||||
_wheelAccelerationAmount = maxAcceleration / 5f;
|
||||
_wheelOffroadCount = 4;
|
||||
}
|
||||
|
||||
private void WheelOffRoad()
|
||||
{
|
||||
_wheelOffroadCount++;
|
||||
}
|
||||
|
||||
private void WheelOnRoad()
|
||||
{
|
||||
_wheelOffroadCount--;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -59,28 +88,23 @@ public class CarLogic : MonoBehaviour
|
||||
}
|
||||
|
||||
_currentAcceleration += accelerationValue * accelerationAmount * Time.deltaTime;
|
||||
|
||||
_currentAcceleration -= brakeValue * brakingAmount * Time.deltaTime;
|
||||
|
||||
// TODO: probably want a brake amount, up to designers to make sure braking out performs acceleration, if desired.
|
||||
_currentAcceleration -= brakeValue * accelerationAmount * Time.deltaTime * 2;
|
||||
|
||||
var newAngle = steeringValue * steeringAnglePerSecond * Time.deltaTime;
|
||||
|
||||
if (_currentAcceleration > 0.01)
|
||||
if (_currentAcceleration > maxAcceleration / 3f)
|
||||
{
|
||||
_transform.Rotate(Vector3.forward, newAngle, Space.World);
|
||||
_currentAcceleration -= _wheelOffroadCount * _wheelAccelerationAmount * Time.deltaTime;
|
||||
}
|
||||
|
||||
// TODO: reverse logic.
|
||||
if (_currentAcceleration < 0)
|
||||
_currentAcceleration = Mathf.Clamp(_currentAcceleration, 0, maxAcceleration);
|
||||
|
||||
if (_currentAcceleration > 0.01)
|
||||
{
|
||||
_currentAcceleration = 0;
|
||||
}
|
||||
|
||||
if (_currentAcceleration > maxAcceleration)
|
||||
{
|
||||
_currentAcceleration = maxAcceleration;
|
||||
var newAngle = steeringValue * steeringAnglePerSecond * Time.deltaTime;
|
||||
_transform.Rotate(Vector3.forward, newAngle, Space.World);
|
||||
}
|
||||
|
||||
_rigidbody.velocity = _transform.up * _currentAcceleration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class CheckPoint : MonoBehaviour
|
||||
{
|
||||
public Action CarPassed;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
CarPassed?.Invoke();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c8ee6d2c6a119049a7f91df5e7712eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(Camera))]
|
||||
public class FovAccelerator : MonoBehaviour
|
||||
{
|
||||
public float minFov;
|
||||
public float maxFov;
|
||||
public CarLogic car;
|
||||
|
||||
private Camera _camera;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Assert.IsTrue(car != null);
|
||||
|
||||
_camera = GetComponent<Camera>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_camera.orthographicSize = Mathf.Lerp(minFov, maxFov, car.CurrentAccelerationRatio());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 548651d9ce9e2ee4a9b5a06f9c50f417
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,75 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class LapTime : MonoBehaviour
|
||||
{
|
||||
public CheckPoint finishLine;
|
||||
public CheckPoint checkPoint1;
|
||||
public CheckPoint checkPoint2;
|
||||
public CheckPoint checkPoint3;
|
||||
|
||||
private float _startTime = -1;
|
||||
private float _checkPoint1Time;
|
||||
private float _checkPoint2Time;
|
||||
private float _checkPoint3Time;
|
||||
private float _finishTime;
|
||||
private float _bestTime;
|
||||
|
||||
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}";
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Start()
|
||||
{
|
||||
finishLine.CarPassed += FinishedLine;
|
||||
checkPoint1.CarPassed += CheckPoint1Passed;
|
||||
checkPoint2.CarPassed += CheckPoint2Passed;
|
||||
checkPoint3.CarPassed += CheckPoint3Passed;
|
||||
_bestTime = float.MaxValue;
|
||||
_finishTime = float.MaxValue;
|
||||
}
|
||||
|
||||
private void CheckPoint1Passed()
|
||||
{
|
||||
_checkPoint1Time = Time.time - _startTime;
|
||||
}
|
||||
|
||||
private void CheckPoint2Passed()
|
||||
{
|
||||
_checkPoint2Time = Time.time - _startTime - _checkPoint1Time;
|
||||
}
|
||||
|
||||
private void CheckPoint3Passed()
|
||||
{
|
||||
_checkPoint3Time = Time.time - _startTime - _checkPoint2Time - _checkPoint1Time;
|
||||
}
|
||||
|
||||
private void ResetTimers()
|
||||
{
|
||||
_startTime = Time.time;
|
||||
_checkPoint1Time = 0;
|
||||
_checkPoint2Time = 0;
|
||||
_checkPoint3Time = 0;
|
||||
if (_finishTime < _bestTime)
|
||||
{
|
||||
_bestTime = _finishTime;
|
||||
}
|
||||
}
|
||||
|
||||
private void FinishedLine()
|
||||
{
|
||||
// make sure we didn't skip a checkpoint.
|
||||
if (_checkPoint1Time > 0 && _checkPoint2Time > 0 && _checkPoint3Time > 0)
|
||||
{
|
||||
// we finished
|
||||
_finishTime = Time.time - _startTime;
|
||||
ResetTimers();
|
||||
}
|
||||
else
|
||||
{
|
||||
ResetTimers();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2df89bc7088dfe646bb46e4bf5096c1b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(Text))]
|
||||
public class LapTimeDisplay : MonoBehaviour
|
||||
{
|
||||
private Text _text;
|
||||
public LapTime _lapTime;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_text = GetComponent<Text>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_text.text = _lapTime.GetTimeString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 365f6a871ab4dce4da64e32f8cf4ecf1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class Wheel : MonoBehaviour
|
||||
{
|
||||
public Action wheelOffRoad;
|
||||
public Action wheelOnRoad;
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other)
|
||||
{
|
||||
wheelOffRoad?.Invoke();
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
wheelOnRoad?.Invoke();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93508d5af5fadde4eac94984663fd7d2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user