2021-02-19 11:29:23 -08:00
|
|
|
using Unity.Mathematics;
|
2021-02-02 15:24:03 -08:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
|
|
public class CarLogic : MonoBehaviour
|
|
|
|
{
|
2021-02-15 18:30:42 -08:00
|
|
|
public InputAction steeringAction;
|
|
|
|
public InputAction accelerationAction;
|
|
|
|
public InputAction brakeAction;
|
2021-02-02 15:24:03 -08:00
|
|
|
|
2021-02-15 18:30:42 -08:00
|
|
|
public float accelerationAmount = 10;
|
|
|
|
[Tooltip("In degrees")]
|
|
|
|
public float steeringAnglePerSecond = 10;
|
2021-02-02 15:24:03 -08:00
|
|
|
|
2021-02-15 18:30:42 -08:00
|
|
|
public float maxAcceleration = 100;
|
2021-02-19 11:29:23 -08:00
|
|
|
public float brakingAmount;
|
2021-02-02 15:24:03 -08:00
|
|
|
|
2021-02-15 18:30:42 -08:00
|
|
|
private Rigidbody2D _rigidbody;
|
|
|
|
private Transform _transform;
|
|
|
|
private float _currentAcceleration;
|
2021-02-19 11:29:23 -08:00
|
|
|
private float _wheelAccelerationAmount;
|
|
|
|
private int _wheelOffroadCount = 0;
|
2021-02-02 15:24:03 -08:00
|
|
|
|
2021-02-19 11:29:23 -08:00
|
|
|
public float CurrentAccelerationRatio()
|
|
|
|
{
|
|
|
|
return _currentAcceleration / maxAcceleration;
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:30:42 -08:00
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
_rigidbody = GetComponent<Rigidbody2D>();
|
|
|
|
_transform = transform;
|
|
|
|
}
|
2021-02-02 15:24:03 -08:00
|
|
|
|
2021-02-15 18:30:42 -08:00
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
steeringAction.Enable();
|
|
|
|
accelerationAction.Enable();
|
|
|
|
brakeAction.Enable();
|
2021-02-19 11:29:23 -08:00
|
|
|
|
|
|
|
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--;
|
2021-02-15 18:30:42 -08:00
|
|
|
}
|
2021-02-02 15:24:03 -08:00
|
|
|
|
2021-02-15 18:30:42 -08:00
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
var steeringValue = steeringAction.ReadValue<float>();
|
|
|
|
var accelerationValue = accelerationAction.ReadValue<float>();
|
|
|
|
var brakeValue = brakeAction.ReadValue<float>();
|
2023-04-01 22:33:01 -07:00
|
|
|
|
2021-02-15 18:30:42 -08:00
|
|
|
if (accelerationValue <= 0)
|
|
|
|
{
|
|
|
|
_currentAcceleration -= _rigidbody.drag * Time.deltaTime;
|
|
|
|
}
|
2021-02-02 15:24:03 -08:00
|
|
|
|
2021-02-15 18:30:42 -08:00
|
|
|
_currentAcceleration += accelerationValue * accelerationAmount * Time.deltaTime;
|
2021-02-19 11:29:23 -08:00
|
|
|
|
|
|
|
_currentAcceleration -= brakeValue * brakingAmount * Time.deltaTime;
|
2021-02-02 15:24:03 -08:00
|
|
|
|
2021-02-19 11:29:23 -08:00
|
|
|
if (_currentAcceleration > maxAcceleration / 3f)
|
2021-02-15 18:30:42 -08:00
|
|
|
{
|
2021-02-19 11:29:23 -08:00
|
|
|
_currentAcceleration -= _wheelOffroadCount * _wheelAccelerationAmount * Time.deltaTime;
|
2021-02-15 18:30:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: reverse logic.
|
2021-02-19 11:29:23 -08:00
|
|
|
_currentAcceleration = Mathf.Clamp(_currentAcceleration, 0, maxAcceleration);
|
|
|
|
|
|
|
|
if (_currentAcceleration > 0.01)
|
2021-02-15 18:30:42 -08:00
|
|
|
{
|
2021-02-19 11:29:23 -08:00
|
|
|
var newAngle = steeringValue * steeringAnglePerSecond * Time.deltaTime;
|
|
|
|
_transform.Rotate(Vector3.forward, newAngle, Space.World);
|
2021-02-15 18:30:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_rigidbody.velocity = _transform.up * _currentAcceleration;
|
|
|
|
}
|
2021-02-19 11:29:23 -08:00
|
|
|
}
|