This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
sota/RunningLateGame/Assets/Scripts/CarController.cs

121 lines
2.7 KiB
C#
Raw Normal View History

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