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?
Answer by jrjr · Dec 19, 2015 at 09:53 AM
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.
Actually 1 more thing - I think I know what you're ai$$anonymous$$g at and want to add a few things to it to save you some serious time if you are not aware of it:
First off you need to quarantine your objects off into sections if you are going to have a lot of them that are able to be knocked around this way. $$anonymous$$ake separate scene sub objects and put those hitable objects into those so you can keep track of them and modify them better.
Second you should be deactivating, removing from the scene, or pooling these kinds of objects or even groups of them or sections of your map that are not going to be in view (if its gonna be a big map). So for example - your big buildings and stuff you can see from far away are LOD based and grouped into a particular bunch that is active when you know they are going to be visible so you can make them so ( this is going to save your balls on occlusion and camera rendering).
You can do similar things for smaller objects (debris, lamp posts, mailboxes, garbage cans,... etc) and have those groups activate on larger trigger based zones.
I'm still only into this less than 2 years but we just got our third multiplayer to run at 60fps with practically no traffic net wise with 400 plus monsters in the scene - very exciting! We're just about to break into big scene creation and the above points are only a portion of whats needed to make it work right.
Feel free to email me at jro$$anonymous$$e2445@gmail.com if ya have any other questions you dont get answered in a day or so I don't check on here much in the past year.
The snippet of code works beautifully and I totally get it! Thanks so much for the help and good luck on your project.
I changed it from looking for a name to looking for a tag I will put on multiple objects. Which will hopefully make it more efficient for me to apply to other assets.