95 lines
2.2 KiB
C#
95 lines
2.2 KiB
C#
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
public class CarLogic : MonoBehaviour
|
|
{
|
|
public InputAction steeringAction;
|
|
public InputAction accelerationAction;
|
|
public InputAction brakeAction;
|
|
|
|
public float accelerationAmount = 10;
|
|
[Tooltip("In degrees")]
|
|
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>();
|
|
_transform = transform;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
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()
|
|
{
|
|
var steeringValue = steeringAction.ReadValue<float>();
|
|
var accelerationValue = accelerationAction.ReadValue<float>();
|
|
var brakeValue = brakeAction.ReadValue<float>();
|
|
|
|
if (accelerationValue <= 0)
|
|
{
|
|
_currentAcceleration -= _rigidbody.drag * Time.deltaTime;
|
|
}
|
|
|
|
_currentAcceleration += accelerationValue * accelerationAmount * Time.deltaTime;
|
|
|
|
_currentAcceleration -= brakeValue * brakingAmount * Time.deltaTime;
|
|
|
|
if (_currentAcceleration > maxAcceleration / 3f)
|
|
{
|
|
_currentAcceleration -= _wheelOffroadCount * _wheelAccelerationAmount * Time.deltaTime;
|
|
}
|
|
|
|
// TODO: reverse logic.
|
|
_currentAcceleration = Mathf.Clamp(_currentAcceleration, 0, maxAcceleration);
|
|
|
|
if (_currentAcceleration > 0.01)
|
|
{
|
|
var newAngle = steeringValue * steeringAnglePerSecond * Time.deltaTime;
|
|
_transform.Rotate(Vector3.forward, newAngle, Space.World);
|
|
}
|
|
|
|
_rigidbody.velocity = _transform.up * _currentAcceleration;
|
|
}
|
|
}
|