/*
* author: ryan lin
* date: TODO
* description: TODO
*/
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
///
/// TODO
///
public class CarController : MonoBehaviour
{
///
/// TODO
///
public enum Axel
{
Front,
Rear
}
///
/// TODO
///
public float acceleration;
///
/// TODO
///
public float turnAngle;
///
/// TODO
///
public List wheels;
///
/// TODO
///
public float brakeForce;
///
/// TODO
///
public bool braking;
///
/// TODO
///
private float _currentAcceleration;
///
/// TODO
///
private float _currentTurn;
///
/// TODO
///
private void FixedUpdate()
{
Move();
Steering();
Brake();
}
///
/// TODO
///
public void SetInputs(float forwardAmount, float turnAmount)
{
_currentAcceleration = forwardAmount * acceleration;
_currentTurn = turnAmount * turnAngle;
}
///
/// TODO
///
public void Move()
{
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
}
///
/// TODO
///
public void Brake()
{
if (braking)
foreach (var wheel in wheels)
wheel.wheelCollider.brakeTorque = brakeForce;
else
foreach (var wheel in wheels)
wheel.wheelCollider.brakeTorque = 0f;
}
///
/// TODO
///
public void Steering()
{
foreach (var wheel in wheels.Where(wheel => wheel.axel == Axel.Front))
wheel.wheelCollider.steerAngle = _currentTurn;
}
///
/// TODO
///
// NOTE: once again, why is this at the bottom lol
[Serializable]
public struct Wheel
{
public GameObject wheelModel;
public WheelCollider wheelCollider;
public Axel axel;
}
}