/* * author: ryan lin * date: 06/08/2024 * description: a controller for the car */ using System; using System.Collections.Generic; using System.Linq; using UnityEngine; /// /// a controller for the car /// public class CarController : MonoBehaviour { /// /// enum for if the wheel is the front or back wheel /// public enum Axel { Front, Rear } /// /// a value for the motor torque that the wheels have when speeding up /// public float motorTorque; /// /// the angle that the wheels turn when the car turns /// public float turnAngle; /// /// a list to input the different wheels /// public List wheels; /// /// the force the wheel exerts when it brakes /// public float brakeForce; /// /// a bool to brake the car /// public bool braking; /// /// inputs for acceleration /// private float _currentAcceleration; /// /// inputs for turning /// private float _currentTurn; /// /// to move the car /// private void FixedUpdate() { Move(); Steering(); Brake(); } /// /// for other scripts to set the inputs /// public void SetInputs(float forwardAmount, float turnAmount) { _currentAcceleration = forwardAmount * motorTorque; _currentTurn = turnAmount * turnAngle; } /// /// to move the car forwards or backwards /// public void Move() { foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration; } /// /// to stop the car /// public void Brake() { if (braking) foreach (var wheel in wheels) wheel.wheelCollider.brakeTorque = brakeForce; else foreach (var wheel in wheels) wheel.wheelCollider.brakeTorque = 0f; } /// /// to turn the car /// public void Steering() { foreach (var wheel in wheels.Where(wheel => wheel.axel == Axel.Front)) wheel.wheelCollider.steerAngle = _currentTurn; } /// /// a struct to display the wheels in the inspector /// [Serializable] public struct Wheel { public GameObject wheelModel; public WheelCollider wheelCollider; public Axel axel; } }