Here’s the link so I don’t have to try and describe it with my monkey brain. Code is below but I’ve tried everything. I’ve tried every combination of rigidbody settings and nothing helped. I’ve moved UpdateWheel() into FixedUpdate, Update and even both at the same time (accidentally). Making the rigidbody kinematic or turning use gravity off just makes the car float like a paper airplane.
Code used for inputs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarControllerV2 : MonoBehaviour
{
public float motorTorque;
public Transform FrontRight;
public Transform FrontLeft;
public Transform BackRight;
public Transform BackLeft;
private VehiclePhysicsV2 FL;
private VehiclePhysicsV2 FR;
private VehiclePhysicsV2 RL;
private VehiclePhysicsV2 RR;
private void Start()
{
FR = FrontRight.gameObject.AddComponent<VehiclePhysicsV2>();
FL = FrontLeft.gameObject.AddComponent<VehiclePhysicsV2>();
RR = BackRight.gameObject.AddComponent<VehiclePhysicsV2>();
RL = BackLeft.gameObject.AddComponent<VehiclePhysicsV2>();
}
private void FixedUpdate()
{
//Vroom
FR.motorTorque = motorTorque;
FL.motorTorque = motorTorque;
//Turn the steering wheel
FR.steerAngle = Input.GetAxis("Horizontal") * 45;
FL.steerAngle = Input.GetAxis("Horizontal") * 45;
}
}
Code attached to the wheels:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VehiclePhysicsV2 : MonoBehaviour
{
private Transform dummyWheel;
private Rigidbody rb;
private Vector3 netForce;
private Vector3 center;
public bool isGrounded;
public float motorTorque;
public float brakeTorque;
public float steerAngle;
public float wheelAngularVelocity;
public float wheelRotationAngle;
public float wheelRadius;
public float wheelMass;
public Color GizmoColor = Color.green;
public Vector3 Center
{
set
{
center = value;
dummyWheel.localPosition = transform.localPosition + center;
}
get
{
return center;
}
}
private void Awake()
{
dummyWheel = new GameObject("DummyWheel").transform;
dummyWheel.transform.parent = this.transform.parent;
Center = Vector3.zero;
wheelRadius = 0.5f;
wheelMass = Mathf.Max(1f, .0001f);
}
private void Start()
{
GameObject parent = this.gameObject;
while (parent != null) //While the gamobject exists
{
if (parent.GetComponent<Rigidbody>() != null) //if there is a rb in this.gameObject.rb
{
rb = parent.GetComponent<Rigidbody>(); //Then set rb to this.gameobject.rb
Debug.Log("Rigidbody set");
break;
}
}
if (rb == null) //If no rb found then error;
{
Debug.Log("VPV1 cant find a rigidbody. Add one to the wheel object.");
//Game freezes and will not respond if under this condition. Force stop is required.
}
}
private void FixedUpdate()
{
UpdateWheel();
}
void UpdateWheel()
{
if (isGrounded)
{
rb.AddForceAtPosition(netForce, transform.position);
}
dummyWheel.localEulerAngles = new Vector3(0, steerAngle, 0); //Set steering angle of dummy
wheelRotationAngle += wheelAngularVelocity * Time.deltaTime; //Calc wheel rotation
this.transform.localEulerAngles = new Vector3(wheelRotationAngle, steerAngle, 0); //Set Rotations of actual wheel
transform.localPosition = dummyWheel.localPosition; //Set position of wheel
wheelAngularVelocity += motorTorque / wheelRadius / wheelMass * Time.deltaTime; //Motor Torque
wheelAngularVelocity -= Mathf.Sign(wheelAngularVelocity) * Mathf.Min(Mathf.Abs(wheelAngularVelocity), brakeTorque * wheelRadius / wheelMass * Time.deltaTime); //Brake Torque
}
public void OnDrawGizmosSelected()
{
Gizmos.color = GizmoColor;
//Draw Suspension
Gizmos.DrawLine(
transform.position - dummyWheel.up * wheelRadius,
transform.position + dummyWheel.up
);
//Draw Wheel
Vector3 point1;
Vector3 point0 = transform.TransformPoint(wheelRadius * new Vector3(0, Mathf.Sin(0), Mathf.Cos(0)));
for (int i = 1; i <= 20; i++)
{
point1 = transform.TransformPoint(wheelRadius * new Vector3(0, Mathf.Sin(i / 20.0f * Mathf.PI * 2.0f), Mathf.Cos(i / 20.0f * Mathf.PI * 2.0f)));
Gizmos.DrawLine(point0, point1);
point0 = point1;
}
Gizmos.color = Color.white;
}
}