Alright, so I have a script for my car which is just the suspension so far. And this script is on the “R32 Multi Raycast”, every time I try testing the car’s suspension it doesn’t work because its too bouncy and I’ve tried changing the values around and it still doesn’t work. But, whenever I duplicated the R32 Car and put the script into each individual wheel transform and for some reason that one works. How can I fix the script so that it works when the script is in the “R32 Multi Raycast” gameobject?
Script:
private Rigidbody rb;
[Header("Suspension")]
public float restLength;
public float springTravel;
public float springStiffness;
public float damperStiffness;
private float minLength;
private float maxLength;
private float lastLength;
private float springLength;
private float springForce;
private float damperForce;
private float springVelocity;
private Vector3 suspensionForce;
[Header("Wheel")]
public float wheelRadius;
public Transform[] wheels;
private Vector3 wheelHubPOS = new Vector3(0, -0.7f, 0);
void Start()
{
rb = GetComponent<Rigidbody>();
minLength = restLength - springTravel;
maxLength = restLength + springTravel;
}
void FixedUpdate()
{
foreach(Transform w in wheels)
{
if (Physics.Raycast(w.position, -w.up, out RaycastHit hit, maxLength + wheelRadius)) {
lastLength = springLength;
springLength = hit.distance - wheelRadius;
springLength = Mathf.Clamp(springLength, minLength, maxLength);
springVelocity = (lastLength - springLength) / Time.fixedDeltaTime;
springForce = springStiffness * (restLength - springLength);
damperForce = damperStiffness * springVelocity;
suspensionForce = (springForce + damperForce) * w.up;
rb.AddForceAtPosition(suspensionForce, hit.point);
}
}
}
Sorry for the messy title didn’t really know what to title it. Thanks in advanced! (Also this script is the one that’s only in the R32 Multi Raycast Empty Gameobject, the only difference with this script and the one inside the wheels is that i don’t use “foreach” i just use “transform.position” to reference the wheel)