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.3 KiB
C#
Raw Normal View History

/*
* author: ryan lin
* date: TODO
* description: TODO
*/
2024-08-02 04:23:06 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
public class CarController : MonoBehaviour
{
/// <summary>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
public enum Axel
{
Front,
Rear
}
/// <summary>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
public float acceleration;
/// <summary>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
public float turnAngle;
/// <summary>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
public List<Wheel> wheels;
/// <summary>
/// TODO
/// </summary>
public float brakeForce;
/// <summary>
/// TODO
/// </summary>
public bool braking;
/// <summary>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
private float _currentAcceleration;
/// <summary>
/// TODO
/// </summary>
private float _currentTurn;
2024-08-02 04:23:06 +00:00
/// <summary>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
private void FixedUpdate()
{
Move();
Steering();
Brake();
}
/// <summary>
/// TODO
/// </summary>
public void SetInputs(float forwardAmount, float turnAmount)
2024-08-02 04:23:06 +00:00
{
_currentAcceleration = forwardAmount * acceleration;
_currentTurn = turnAmount * turnAngle;
2024-08-02 04:23:06 +00:00
}
/// <summary>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
public void Move()
{
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
}
/// <summary>
/// TODO
/// </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>
/// TODO
/// </summary>
2024-08-02 04:23:06 +00:00
public void Steering()
{
foreach (var wheel in wheels)
if (wheel.axel == Axel.Front)
wheel.wheelCollider.steerAngle = _currentTurn;
}
/// <summary>
/// TODO
/// </summary>
// NOTE: once again, why is this at the bottom lol
2024-08-02 04:23:06 +00:00
[Serializable]
public struct Wheel
{
public GameObject wheelModel;
public WheelCollider wheelCollider;
public Axel axel;
}
}