Super simple rigid body destruction, how?

You know in GTA or the older midtown madness games you can drive into a lamp post or other objects and they would fall over? If I just use a normal rigid body it will fall over without being hit so how would I only activate a rigid body after it has been hit with, say, a collider from any other object?

1 Answer

1

This might not be the best method but should work.
I just tested it and it does. I was thinkin this same thing about 3 weeks ago while messing around with GTA 5 for first time.

For test - Make two cubes. In blank scene. The first one is your mover (car or person or whatever). The first one should have a kinimatic (for test purposes at least) rigidbody and a collider.

The second cube just has a box collider set to trigger. You shouldnt even need a actual collider on it (other than the trigger, i dont think this affects performance now with 5.x but that’s a hunch after lots of messing with it).

Then you do -
void OnTriggerEnter(Collider other){
if (other.gameObject.name.Contains(“Cube”)){
other.gameObject.AddComponent ();
}

You have to add rigidbody with greater than less than signs around it right after addcomponent… it’s not showing up in the comment sorry.
In a small script on the kinimatic cube.

Hit play, drag your cube with kinimatic rigid body on it into the other cube and it will fall as you are wanting.

I don’t know about any static checkbox effects with this that could be bad but if you have it marked static your mesh will stay where it is and your collider will fall (which will make it look like it didnt work unless you have it selected. Just wanted to mention that because it looks damn confusing at first but makes sense.

After your objects done bouncing around you should have something in a script on it to destroy both the rigidbody (if you don’t want it to move anymore) and the script that’s going to remove that rigid body itself). You could also just keep track of those objects in a list on your kinematic moving body and just deactivate those later though (might save some script room, depends on your situation).

j

I haven't tested turning off static at runtime yet... actually I just hit a static cube with the above test and the mesh followed. Odd. I've seen this not work before perhaps its something recently added or to do with convex meshes (mainly what i was working with at the time). Just something to look out for. Good luck man.

–

The snippet of code works beautifully and I totally get it! Thanks so much for the help and good luck on your project.

–

No, what I want to do is don't let the player pass through a block. For example, stay over the floor made of blocks. The player doesn't fall because I set the gravity to 0, intentionally. I want to move the player around the screen but only when I push the arrow keys, and I want to put obstacles around to don't let the player from go that way.

–