Skip to content

Instantly share code, notes, and snippets.

@ThaDewey
Created June 24, 2017 04:59
Show Gist options
  • Save ThaDewey/7ec24293495d1110fecc5ef0c23dbb75 to your computer and use it in GitHub Desktop.
Save ThaDewey/7ec24293495d1110fecc5ef0c23dbb75 to your computer and use it in GitHub Desktop.
Rollbar physics hanlder so that physically driven cars don;t flip over. use as componeennt
//http://answers.unity3d.com/questions/21417/my-car-keeps-fliping-over-i-have-an-idea-how-to-fi.html
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment