(Moved from the physics sections, but still my post)So I’m trying to delve into the world of physics in unity, since I’ve messed around enough in other parts(trying to improve my knowledge)and wanted to make a kinda decently featured car controller, and it led me to following a couple of tutorials, ultimately ending up with this code. Uses standard unity wheel colliders.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VehicleController : MonoBehaviour
{
[Header("Vehicle Components")]
public WheelCollider[] wheels = new WheelCollider[4];
public Transform[] wheelMeshes = new Transform[4];
public Transform CenterOfMass;
public AnimationCurve enginePower;
[Header("Vehicle Settings")]
public float vehicleTorque;
public float brakePower;
public float steeringAngle;
public float radius;
public float downforce;
public float maxRPM;
public float minRPM;
public float engineRPM;
public float smoothTime = 0.01f;
public float[] gears;
public int gearNum = 0;
private InputManager inputManager;
private Rigidbody rb;
private bool reverse;
[Header("Vehicle Debug")]
public float vehicleSpeed;
public float wheelsRPM;
private void Start()
{
inputManager = GetComponent<InputManager>();
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
SetSteering();
SetWheels();
SetDownforce();
SetBrake();
SetMisc();
SetShift();
CalculateEnginePower();
}
private void SetForces()
{
for (int i = 0; i < wheels.Length; i++)
{
wheels[i].motorTorque = vehicleTorque;
}
}
private void SetSteering()
{
if (inputManager.horizontalInput > 0)
{
wheels[0].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius + (1.5f / 2))) * inputManager.horizontalInput;
wheels[1].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius - (1.5f / 2))) * inputManager.horizontalInput;
}
else if (inputManager.horizontalInput < 0)
{
wheels[0].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius - (1.5f / 2))) * inputManager.horizontalInput;
wheels[1].steerAngle = Mathf.Rad2Deg * Mathf.Atan(2.55f / (radius + (1.5f / 2))) * inputManager.horizontalInput;
}
else
{
wheels[0].steerAngle = 0;
wheels[1].steerAngle = 0;
}
}
private void SetWheels()
{
Vector3 wheelPos = Vector3.zero;
Quaternion wheelRot = Quaternion.identity;
for (int i = 0; i < 4; i++)
{
wheels[i].GetWorldPose(out wheelPos, out wheelRot);
wheelMeshes[i].transform.position = wheelPos;
wheelMeshes[i].transform.rotation = wheelRot;
}
}
private void SetDownforce()
{
rb.AddForce(-Vector3.up * downforce * rb.velocity.magnitude);
}
private void SetBrake()
{
if (inputManager.handbrake)
{
wheels[0].brakeTorque = wheels[1].brakeTorque = wheels[2].brakeTorque = wheels[3].brakeTorque = brakePower;
}
else
{
wheels[0].brakeTorque = wheels[1].brakeTorque = wheels[2].brakeTorque = wheels[3].brakeTorque = 0;
}
}
private void CalculateEnginePower()
{
GetRPM();
vehicleTorque = enginePower.Evaluate(engineRPM) * (gears[gearNum]) * inputManager.verticalInput;
float velocity = 0f;
engineRPM = Mathf.SmoothDamp(engineRPM, 1000 + (Mathf.Abs(wheelsRPM) * 3.6f * (gears[gearNum])), ref velocity, smoothTime);
engineRPM = engineRPM / 2;
SetForces();
}
private void GetRPM()
{
float sum = 0;
int R = 0;
for (int i = 0; i < 4; i++)
{
sum += wheels[i].rpm;
R++;
}
wheelsRPM = (R != 0) ? sum / R : 0;
if (wheelsRPM < 0 && !reverse)
{
reverse = true;
}
else if (wheelsRPM > 0 && reverse)
{
reverse = false;
}
}
private void SetMisc()
{
vehicleSpeed = rb.velocity.magnitude * 3.6f;
rb.centerOfMass = CenterOfMass.localPosition;
}
private void SetShift()
{
if (engineRPM > maxRPM && gearNum < gears.Length-1 && !reverse)
{
gearNum++;
}
if (engineRPM < minRPM && gearNum > 0)
{
gearNum--;
}
}
}
Input Manager is extremely simple
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputManager : MonoBehaviour
{
public float horizontalInput;
public float verticalInput;
public bool handbrake;
private void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
handbrake = Input.GetButton("Jump");
}
}
However, this code does often bug, especially with the engine RPM, which can sometimes fluctuate in multiple places causing undesired effects, and gears sometimes get stuck and don’t move, among other issues. Another very critical issue is it would cycle through the gears very quickly. I would also like to know if there’s any fixes and immediate problems that anybody sees with the code, as well as any improvements you could suggest? I understand this may not be the best way to do it, but I’ll at least give it a shot.