Help center of mass.

hi all,i’ve made a little script for my car,but i’ve a problem on the center of mass,i move it via script:

var shiftCenter : Vector3 = Vector3(0.0, -1.0, 0.0);

function Start () {
rigidbody.centerOfMass += shiftCenter;
Debug.Log(rigidbody.centerOfMass);

}


function FixedUpdate() {
//accension code

Debug.Log(rigidbody.centerOfMass);

}

when i start th debug show correct value,but if i turn on my car, the center of mass returns to default veicle center and dont where i want.

nex problem is when i steer, my car is instable and flip…

Try something like

function Start() {
rigidbody.centerOfMass.y = -1.0;
washCar = true;//lol
}

This way you won’t need to use the vector var. This should help your car from not flipping over, and if that doesn’t do anything try the mass of the wheel colliders.

don’t work. center of mass returns always in the center of mesh.is possible is a bug?

Have you tried just assigning a value to the centerOfMass property? I guess it’s possible that the += operator might cause problems.

yes,i’ve tried some ways,but problem persist.make me crazy…i don’t understand why the centor of mass is moving and don’t stay where i put.

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.

And finally, I found out that the centerOfMass, update’s its self when you give it more colliders… SO!!!, the rigidbody.centerOfMass=centerOfMass; should always appear at the end of your startup. :wink:

wow,thank’s.i think the problem is or not correct collider(i use mesh collider)or i have noi idea.

naw, easier, just have it dot he center of mass after you apply the wheel colliders and all other colliders. (at the end of the Start function)