- Home /
My simple car leans INTO the turn, and leans over on two wheels.
My simple car (box collider with 4 wheel colliders) will lean over on the two inside wheels when turning. It is a rigidbody, and I have been following Flat Tutorial's car tutorial to a T.
Here is a picture of the problem:
And here is the control code (javascript)
var wheelFL : WheelCollider;
var wheelFR : WheelCollider;
var wheelBL : WheelCollider;
var wheelBR : WheelCollider;
var wheelFLTrans : Transform;
var wheelFRTrans : Transform;
var wheelBLTrans : Transform;
var wheelBRTrans : Transform;
var highestSpeed : float = 50;
var lowSpeedSteerAngle : float = 10;
var highSpeedSteerAngle : float = 1;
var decelerationSpeed : float = 10;
var maxTorque : float = 50;
function Start () {
rigidbody.centerOfMass += Vector3(0, 0, 1.0);
}
function FixedUpdate () {
wheelBR.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelBL.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelFR.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelFL.motorTorque = maxTorque * Input.GetAxis("Vertical");
if (Input.GetButton("Vertical") == false) {
wheelBR.brakeTorque = decelerationSpeed;
wheelBL.brakeTorque = decelerationSpeed;
wheelFR.brakeTorque = decelerationSpeed;
wheelFL.brakeTorque = decelerationSpeed;
}
else {
wheelBR.brakeTorque = 0;
wheelBL.brakeTorque = 0;
wheelFR.brakeTorque = 0;
wheelFL.brakeTorque = 0;
}
var speedFactor = rigidbody.velocity.magnitude/highestSpeed;
var currentSteerAngle = Mathf.Lerp(lowSpeedSteerAngle,highSpeedSteerAngle,speedFactor);
currentSteerAngle = Input.GetAxis("Horizontal");
wheelFL.steerAngle = 10 * currentSteerAngle;
wheelFR.steerAngle = 10 * currentSteerAngle;
}
moreleaningprobs.png
(196.1 kB)
Comment
Answer by Cyril5 · Jun 08, 2014 at 03:22 PM
Hello
Instead of use Vector3 to stabilize my car I used centerOfMass.y :
var centerOfmass = -1;
// find correct value to stabilize your car (between -1 & 1 : example -0.95)
function Start() {
rigidbody.centerOfMass.y = centerOfmass;
}