2024-08-10 02:25:46 +08:00
|
|
|
/*
|
2024-08-15 16:25:18 +08:00
|
|
|
* author: ryan lin, mark joshwel
|
2024-08-11 05:40:22 +08:00
|
|
|
* date: 06/08/2024
|
|
|
|
* description: a controller for the car
|
2024-08-10 02:25:46 +08:00
|
|
|
*/
|
|
|
|
|
2024-08-02 12:23:06 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2024-08-10 02:28:26 +08:00
|
|
|
using System.Linq;
|
2024-08-02 12:23:06 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// a controller for the car
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-15 16:25:18 +08:00
|
|
|
public class VehicleController : MonoBehaviour
|
2024-08-02 12:23:06 +08:00
|
|
|
{
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// enum for if the wheel is the front or back wheel
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-02 12:23:06 +08:00
|
|
|
public enum Axel
|
|
|
|
{
|
|
|
|
Front,
|
|
|
|
Rear
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// a value for the motor torque that the wheels have when speeding up
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
public float motorTorque;
|
2024-08-10 02:25:46 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2024-08-11 13:51:29 +08:00
|
|
|
/// the angle that the wheels turn when the car turns
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-02 12:23:06 +08:00
|
|
|
public float turnAngle;
|
2024-08-10 02:25:46 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// a list to input the different wheels
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-02 12:23:06 +08:00
|
|
|
public List<Wheel> wheels;
|
2024-08-05 17:56:43 +08:00
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// the force the wheel exerts when it brakes
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-05 17:56:43 +08:00
|
|
|
public float brakeForce;
|
2024-08-10 02:25:46 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// a bool to brake the car
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-05 17:56:43 +08:00
|
|
|
public bool braking;
|
2024-08-10 02:25:46 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// inputs for acceleration
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-15 16:25:18 +08:00
|
|
|
[SerializeField] private float currentAcceleration;
|
2024-08-02 12:23:06 +08:00
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// inputs for turning
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-15 16:25:18 +08:00
|
|
|
[SerializeField] private float currentTurn;
|
2024-08-02 12:23:06 +08:00
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// to move the car
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-02 12:23:06 +08:00
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
|
|
|
Move();
|
|
|
|
Steering();
|
|
|
|
Brake();
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// for other scripts to set the inputs
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-05 17:56:43 +08:00
|
|
|
public void SetInputs(float forwardAmount, float turnAmount)
|
2024-08-02 12:23:06 +08:00
|
|
|
{
|
2024-08-15 16:25:18 +08:00
|
|
|
currentAcceleration = forwardAmount * motorTorque;
|
|
|
|
currentTurn = turnAmount * turnAngle;
|
2024-08-02 12:23:06 +08:00
|
|
|
}
|
2024-08-05 17:56:43 +08:00
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// to move the car forwards or backwards
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-15 16:25:18 +08:00
|
|
|
private void Move()
|
2024-08-02 12:23:06 +08:00
|
|
|
{
|
2024-08-15 02:31:58 +08:00
|
|
|
foreach (var wheel in wheels)
|
2024-08-15 16:25:18 +08:00
|
|
|
wheel.wheelCollider.motorTorque = currentAcceleration;
|
2024-08-02 12:23:06 +08:00
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// to stop the car
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-15 16:25:18 +08:00
|
|
|
private void Brake()
|
2024-08-02 12:23:06 +08:00
|
|
|
{
|
|
|
|
if (braking)
|
|
|
|
foreach (var wheel in wheels)
|
|
|
|
wheel.wheelCollider.brakeTorque = brakeForce;
|
|
|
|
else
|
|
|
|
foreach (var wheel in wheels)
|
|
|
|
wheel.wheelCollider.brakeTorque = 0f;
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// to turn the car
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-15 16:25:18 +08:00
|
|
|
private void Steering()
|
2024-08-02 12:23:06 +08:00
|
|
|
{
|
2024-08-15 16:25:18 +08:00
|
|
|
foreach (var wheel in wheels.Where(wheel => wheel.axel == Axel.Front))
|
|
|
|
wheel.wheelCollider.steerAngle = currentTurn;
|
2024-08-02 12:23:06 +08:00
|
|
|
}
|
|
|
|
|
2024-08-10 02:25:46 +08:00
|
|
|
/// <summary>
|
2024-08-11 05:40:22 +08:00
|
|
|
/// a struct to display the wheels in the inspector
|
2024-08-10 02:25:46 +08:00
|
|
|
/// </summary>
|
2024-08-02 12:23:06 +08:00
|
|
|
[Serializable]
|
|
|
|
public struct Wheel
|
|
|
|
{
|
|
|
|
public GameObject wheelModel;
|
|
|
|
public WheelCollider wheelCollider;
|
|
|
|
public Axel axel;
|
|
|
|
}
|
|
|
|
}
|