I want to add anti-roll bars to my car to keep it from flipping. how would i do this? if you dont know what an anti-roll bar is heres a description.
Stabilizer bars (A.K.A. anti-roll bars) "connect" the two wheels of the same axle allowing a limited degree of freedom between the two. When one of the wheels is pushed upwards, the stabilizer bar transfers a portion of that compression force to the other wheel, so its suspension compress as well. This limits the roll of the body's car at that axle thus keeping it from flipping.
the script is really simple
in Javascript :
public class AntiRollBar : MonoBehaviour
{
public WheelCollider WheelL;
public WheelCollider WheelR;
public float AntiRoll = 5000.0f;
public void FixedUpdate()
{
WheelHit hit;
float travelL = 1.0f;
float travelR = 1.0f;
bool groundedL = WheelL.GetGroundHit(out hit);
if (groundedL)
travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
bool groundedR = WheelR.GetGroundHit(out hit);
if (groundedR)
travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;
float antiRollForce = (travelL - travelR) * AntiRoll;
if (groundedL)
rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
WheelL.transform.position);
if (groundedR)
rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce,
WheelR.transform.position);
}
}
in C#
using UnityEngine;
using System.Collections;
public class AntiRollBar : MonoBehaviour {
public WheelCollider wheelL;
public WheelCollider wheelR;
public float antiRollVal = 5000f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
WheelHit hit;
float travelL=1.0f;
float travelR=1.0f;
bool groundedL = wheelL.GetGroundHit(out hit);
if (groundedL){
travelL = (-wheelL.transform.InverseTransformPoint(hit.point).y - wheelL.radius) / wheelL.suspensionDistance;
}
bool groundedR = wheelR.GetGroundHit(out hit);
if (groundedR){
travelR = (-wheelR.transform.InverseTransformPoint(hit.point).y - wheelR.radius) / wheelR.suspensionDistance;
}
float antiRollForce = (travelL - travelR) * antiRollVal;
if (groundedL)
rigidbody.AddForceAtPosition(wheelL.transform.up * -antiRollForce,
wheelL.transform.position);
if (groundedR)
rigidbody.AddForceAtPosition(wheelR.transform.up * antiRollForce,
wheelR.transform.position);
}
}
I think it is better to execute both that
rigidbody.AddForceAtPosition(wheelL.transform.up * -antiRollForce,
wheelL.transform.position);
rigidbody.AddForceAtPosition(wheelR.transform.up * antiRollForce,
wheelR.transform.position);
when either groundedR or groundedL are true because when one wheel is on the ground and one not only one condition is executed.
I found a script that simulates anti roll bars :D. if u want a link to the web page pm me.