- Home /
How do I set the center of mass for my vehicle?
Im making a racing game and 7/8of my cars are working perfectly: what I mean is, they are driving well, their center of mass is good, but it comes down to one vehicle. See, im using the catamount car from unitys car tutorial asset with my own script. The driving is working well, but the problem is, whenever I turn hard to right/left, it just bicycles.(goes on 2 wheels.) I dont know,if its the COM, or a problem with wheel colliders. Heres a snippet of my COM code so you can see.
#pragma strict
var centerOfMass : Vector3
function Start (){
GetComponent.<Rigidbody>.centerOfmass = centerOfMass;
SetValues();
}
I assume you set the centerOf$$anonymous$$ass values to something like (0, -0.5f, 0), right? Anyway, imagine sitting in a real car, driving fast and suddenly turning hard left or right, wouldn't the car go on two wheels or even flip? It's (kind of) realistic behavior, I would think. Also, I don't know about the car set up of the tutorial, but a real car also hass a suspension that helps in those cases.
Lowering center of mass is not best solution for making car stable if you want realistic car physics, but for arcade its ok. If you try turn at high speed you need reduce max angle of wheel rotation, so your max angle base on curent speed of car.
$$anonymous$$y center mass is -0.5,-1.2,0. This works fine for me, but if i set the suspens ion to 0. But even this causes the car to act really weird. Also i forgot to mention that it lifts up even at lower speeds. I just need an, if you could, a solution, like increasing the mass of the wheel colliders, or maybe increasing the rigidbody mass, or interpolation? Thx anyway
Answer by UziMonkey · Jun 28, 2015 at 10:17 PM
The easiest way to do this is add a child empty object called Com or something. Then in your script set the empty's local position to the rigid body's center off mass. This makes it easily adjustable and not hardcoded, and the script that sets the center off mass will be reusable. Just remember to use localPosition, as the center of mass property will only work with local coodinates.
I couldn't reposition my vehicle because it was twisting like a worm. It was from using position instead of localPosition. Thanks!