I have no idea what’s going on, my car just bounces up dramatically for seemingly no reason when I have this code on:
public class StabilizerBars : MonoBehaviour {
WheelCollider FL, FR, BL, BR;
float AntiWheelForce = 200;
public float TravelL = 1.0f;
public float TravelR = 1.0f;
public float AntiRollForce;
// Use this for initialization
void Start () {
FL = GameObject.Find ("Wheel_FL").GetComponent<WheelCollider>();
FR = GameObject.Find ("Wheel_FR").GetComponent<WheelCollider>();
BL = GameObject.Find ("Wheel_BL").GetComponent<WheelCollider>();
BR = GameObject.Find ("Wheel_BR").GetComponent<WheelCollider>();
}
// Update is called once per frame
void Update () {
WheelHit hit;
bool GroundL = FL.GetGroundHit(out hit);
bool GroundR = FR.GetGroundHit(out hit);
if (GroundL) {
TravelL = (-FL.transform.InverseTransformPoint (hit.point).y - FL.radius) / FL.suspensionDistance;
}
if (GroundR) {
TravelR = (-FR.transform.InverseTransformPoint(hit.point).y - FR.radius) / FR.suspensionDistance;
}
AntiRollForce = (TravelL - TravelR) * AntiWheelForce;
print (AntiRollForce);
if (GroundL) {
gameObject.rigidbody.AddForceAtPosition(
FL.transform.up * -AntiRollForce,
FL.transform.position);
}
if (GroundR) {
gameObject.rigidbody.AddForceAtPosition(
FR.transform.up * AntiRollForce,
FR.transform.position);
}
}
}
It also doesn’t stablize the car at all.
For now it’s only enabled for the front wheels. I made it do the back wheels earlier and the same thing happened.
Does anyone know why this happens?