- Home /
Best way to add a MeshCollider at runtime without ruining physics?
Hey! I need to add a meshcollider to my vehicle at runtime but as soon as I do it will ruin physics entirely. The car will clip at 20mph, and roll over 4 or 5 times. Not it is not stabilizer bars, yes I am adding them, yes the CoG is correct.
Here is a snippet of the code
//RIGIDBODY FIRST!
car.AddComponent<Rigidbody>();
Rigidbody r = car.GetComponent<Rigidbody>();
r.interpolation = RigidbodyInterpolation.Interpolate;
r.collisionDetectionMode = CollisionDetectionMode.Continuous;
car.AddComponent<MeshCollider>();
MeshCollider CCgb = car.GetComponent<MeshCollider>();
CCgb.convex = true;
//CAR COLLISION!
Mesh carColliderMSH = tmdl_load.LoadTML (CarFile + "/" + CarName + "_COLLISION.tml",true,false).GetComponent<MeshCollider>().sharedMesh;
CCgb.sharedMesh = carColliderMSH; //This messes it up =c
//Calc physics?
car.SetActive (false);
car.SetActive (true);
Comment
Answer by The_Mean_Fiddler · Jun 20, 2015 at 06:09 PM
The center of gravity of the rigid body is calculated from the colliders attached to it.
You will have to adjust the position of the center or gravity. eg.
rigidbody.centerOfMass += new Vector3(0,-0.3f,1f);