I tried to recreate your problem. I created a box (well 2 boxes cos 1 just didn’t look right) parented them put 4 cylinders under it and called them wheels. Then proceeded with this script:
var mass=2000.0;
var power=5000.0;
var maxSteer=45;
var WheelFR:Transform;
var WheelFL:Transform;
var WheelRR:Transform;
var WheelRL:Transform;
var centerOfMass=Vector3(0,0,1.0);
function Start(){
print("Start: " + rigidbody.centerOfMass);
//rigidbody.centerOfMass+=centerOfMass;
rigidbody.mass=mass;
setupWheel(WheelFR, true);
setupWheel(WheelFL, true);
setupWheel(WheelRR, false);
setupWheel(WheelRL, false);
}
function Update () {
var steer=Input.GetAxis("Horizontal") * maxSteer;
var power=Input.GetAxis("Vertical") * power * Time.deltaTime;
WheelFR.collider.steerAngle=steer;
WheelFL.collider.steerAngle=steer;
WheelRR.collider.motorTorque=power;
WheelRL.collider.motorTorque=power;
print("Update: " + rigidbody.centerOfMass);
}
function setupWheel(wheel : Transform, isFront : boolean){
var m=wheel.parent.rigidbody.mass;
var r=wheel.collider.radius;
//wheel.collider.mass=50;
wheel.collider.suspensionDistance=r;
wheel.collider.suspensionSpring.spring=m/(isFront?2:4);
wheel.collider.suspensionSpring.damper=2;
wheel.collider.suspensionSpring.targetPosition=r;
}
My notes are as follows:
The initial center of mass is derived by the position of the various colliders which make up the model. So in the case of my little model it was (0.0, 0.1, -0.5)
When I un-comment the line //rigidbody.centerOfMass+=centerOfMass;… When the model hits the ground it violently shakes and falls through the ground.
So… I did some more testing and got rid of some of the pork in the project and got it down to this:
var mass=2000.0;
var power=5000.0;
var maxSteer=20;
var WheelFR:Transform;
var WheelFL:Transform;
var WheelRR:Transform;
var WheelRL:Transform;
var centerOfMass=Vector3(0,0,0);
function Start(){
//print("Start: " + rigidbody.centerOfMass);
rigidbody.centerOfMass=centerOfMass;
rigidbody.mass=mass;
}
function Update () {
var steer=Input.GetAxis("Horizontal") * maxSteer;
var power=Input.GetAxis("Vertical") * power * Time.deltaTime;
WheelFR.collider.steerAngle=steer;
WheelFL.collider.steerAngle=steer;
WheelRR.collider.motorTorque=power;
WheelRL.collider.motorTorque=power;
//print("Update: " + rigidbody.centerOfMass);
}
Ran it and with the same line commented, worked just fine, when I un-commented it… still did the same thing.
THEN!!! I commented: rigidbody.mass=mass; and ran it, it didn’t do it… In a brainstorm, I swapped the two lines…
rigidbody.mass=mass;
rigidbody.centerOfMass=centerOfMass;
Now, with both lines un-commented it works perfectly…
I suppose this error can be reproduced. I don’t know if this is your problem but it certainly is a problem.
I think this is caused by when you define the center of mass it does all the calculations for creating the rotation of the object, but does not do the same when you define the mass.