Hi, i’m making a racing game, and i made a car controller script. It works well but i want to add an automatic gearbox to it. How do i do it ?
PS : i want it to shift according to the engine RPM (for example if the engine reaches 100 RPM it switches to second speed) and i want to be able to edit the number of speeds (all the cars in the game doesn’t have the same amount of speeds)
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
public enum Axel
{
Front,
Middle,
Rear
}
public enum driveType
{
frontWheelDrive,
rearWheelDrive,
allWheelDrive
}
[Serializable]
public struct Wheel
{
public GameObject model;
public WheelCollider collider;
public Axel axel;
}
public enum drivetype
{
allWheelDrive = 1, rearWheelDrive = 2, frontWheelDrive = 3
}
[RequireComponent(typeof(InputManager))]
public class PlayerCarController : MonoBehaviour
{
public driveType DriveType;
public InputManager inputManager;
public List<Wheel> wheels;
public Rigidbody localRigidbody;
public Vector3 centerOfMass;
//Throttle System variables
public float torque = 1000f;
public float maxAcceleration = 20f;
public float finalMaxAcceleration;
public float currentSpeed;
public float topSpeed /*= 200f*/;
public float topSpeedValue = 200;
//Steering system variables
public float turnSensitivity = 1f;
public float maximumTurnAngle = 45f;
//Hand brake system variables
public bool handBrakeIsOn;
//nitro system variables
public bool nitroIsOn;
public float nitroValue = 1;
void Start()
{
localRigidbody = GetComponent<Rigidbody>();
localRigidbody.centerOfMass = centerOfMass;
}
private void Update()
{
finalMaxAcceleration = maxAcceleration * nitroValue;
currentSpeed = localRigidbody.velocity.sqrMagnitude;
//Debug.Log(currentSpeed + " | " + topSpeed);
}
void FixedUpdate()
{
AnimateWheels();
ThrottleSystem();
SteeringSystem();
HandBeakeSystem();
NitroSystem();
TopSpeedDefinition();
}
void ThrottleSystem()
{
foreach (var wheel in wheels)
{
if (DriveType == driveType.allWheelDrive)
{
wheel.collider.motorTorque = inputManager.throttle * finalMaxAcceleration * torque * Time.deltaTime;
}
if (DriveType == driveType.rearWheelDrive)
{
if(wheel.axel == Axel.Rear)
{
wheel.collider.motorTorque = inputManager.throttle * finalMaxAcceleration * torque * Time.deltaTime;
}
}
if (DriveType == driveType.frontWheelDrive)
{
if (wheel.axel == Axel.Front)
{
wheel.collider.motorTorque = inputManager.throttle * finalMaxAcceleration * torque * Time.deltaTime;
}
}
}
}
void TopSpeedDefinition()
{
foreach (var wheel in wheels)
{
if (currentSpeed > topSpeed)
{
wheel.collider.motorTorque = 0f;
}
else if (currentSpeed < topSpeed)
{
wheel.collider.motorTorque = inputManager.throttle * finalMaxAcceleration * torque * Time.deltaTime;
}
}
}
void SteeringSystem()
{
foreach (var wheel in wheels)
{
if (wheel.axel == Axel.Front)
{
var _steerAngle = inputManager.steer * turnSensitivity * maximumTurnAngle;
wheel.collider.steerAngle = Mathf.Lerp(wheel.collider.steerAngle, _steerAngle, 0.5f);
}
}
}
void HandBeakeSystem()
{
if (inputManager.handBrake)
{
handBrakeIsOn = true;
}
else
{
handBrakeIsOn = false;
}
foreach (var wheel in wheels)
{
if(handBrakeIsOn == true)
{
wheel.collider.brakeTorque = 1000000000f;
wheel.collider.motorTorque = 0f;
}
else if(handBrakeIsOn == false)
{
wheel.collider.brakeTorque = 0f;
wheel.collider.motorTorque = inputManager.throttle * finalMaxAcceleration * torque * Time.deltaTime;
}
}
}
void NitroSystem()
{
if(inputManager.nitro)
{
nitroIsOn = true;
}
else
{
nitroIsOn = false;
}
if(nitroIsOn == true)
{
nitroValue = 5f;
topSpeed = topSpeedValue * 2f;
}
else if (nitroIsOn == false)
{
nitroValue = 1f;
topSpeed = topSpeedValue;
}
}
void AnimateWheels()
{
foreach (var wheel in wheels)
{
Vector3 wheelColliderPosition;
Quaternion WheelColliderRotation;
wheel.collider.GetWorldPose(out wheelColliderPosition, out WheelColliderRotation);
wheel.model.transform.position = wheelColliderPosition;
wheel.model.transform.rotation = WheelColliderRotation;
}
}
}