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/VehicleController.cs

121 lines
2.8 KiB
C#
Raw Normal View History

/*
2024-08-15 08:25:18 +00:00
* author: ryan lin, mark joshwel
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;
/// <summary>
2024-08-10 21:40:22 +00:00
/// a controller for the car
/// </summary>
2024-08-15 08:25:18 +00:00
public class VehicleController : MonoBehaviour
2024-08-02 04:23:06 +00:00
{
/// <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-11 05:51:29 +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-15 08:25:18 +00:00
[SerializeField] private float currentAcceleration;
2024-08-02 04:23:06 +00:00
/// <summary>
2024-08-10 21:40:22 +00:00
/// inputs for turning
/// </summary>
2024-08-15 08:25:18 +00:00
[SerializeField] 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-15 08:25:18 +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-15 08:25:18 +00:00
private void Move()
2024-08-02 04:23:06 +00:00
{
foreach (var wheel in wheels)
2024-08-15 08:25:18 +00:00
wheel.wheelCollider.motorTorque = currentAcceleration;
2024-08-02 04:23:06 +00:00
}
/// <summary>
2024-08-10 21:40:22 +00:00
/// to stop the car
/// </summary>
2024-08-15 08:25:18 +00:00
private void Brake()
2024-08-02 04:23:06 +00:00
{
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-15 08:25:18 +00:00
private void Steering()
2024-08-02 04:23:06 +00:00
{
2024-08-15 08:25:18 +00:00
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;
}
}