- Home /
Transition Between Regular Animations and Ragdoll Physics
Hi everybody,
I know how to create animations, and I know how to set up rag doll physics on a character. My main question is how do I write a code that ends regular animations (walking, running, fighting etc.) upon dying, and transitions into a rag doll effect on death.
Alternatively, I'd like to assign certain health to certain body part, and have it respond to rag doll physics without requiring the character to die. In other words, if the character gets shot in the head, I want his head to swing back, and snap back into place. Similarly, I would like that to happen to shoulders, arms, chest, and legs.
Thanks for your help
The certain body parts moving independently on hit should be done with animations, while the ragdoll (I'm assu$$anonymous$$g you have it set up) can be done by disabling the rigidbodys when player is alive and enabling them on death.
Answer by Benproductions1 · Mar 21, 2013 at 11:26 PM
Hello again,
If you want to disable all rigidbody's, you can simple do:
function DisableRagdoll() {
var rb:Rigidbody;
for (rb in GetComponentsInChildren(Rigidbody)) {
rb.enabled = false;
}
}
To enable it, just replace rb.enabled = false;
with rb.enabled =true;
Hope this helps, Benproductions1
I don't understand what this should do? Do you want this to happen when he dies, in which case it should be hitPoints < 0
or do you want this to happen while he's not dead? In which case Destroy
makes no sense what do ever
@Benproductions1 this is just a test. I don't want to go through the trouble of injuring the enemy, so I make the conditions for this to trigger happen as soon as the program begins. As of now, all that happens is that the enemy is destroyed. If I wrote the script correctly, it would look like yours, but I'm using a short cut.
@Dr_GeoFry So what does this code accomplish? It freezes all rigidbodys in the rigidbodys
list and destroys said rigidbody in 3 seconds, then destroys the gameObject that the script is attached to in 12 seconds. then it makes a variable called enabled equal to false
. I can't tell you where you went wrong, before I know what it's supposed to do! :P
lol, @Benproductions1 I was throwing all kinds of variables at this hoping to get the code to destroy all of the rigidbodies. $$anonymous$$y goal with this code was to have the animation play for X amount of time, then destroy the rigidbodies, and then remove the enemy altogether. I thought that I needed to either destroy or disable rigidbodies or disable is$$anonymous$$inematic
in order to make the ragdoll effect happen. That, or make is$$anonymous$$inematic
equal false. It seemed like this transition should be easy, but for some reason, I'm missing some part of this that is critical. Thank you for your help.
For the ragdoll effect, you need to enable the rigidbodys, not destroy them! Therefore is$$anonymous$$inematic has to be false and/or enable the component. Also you should use {}
to wrap code:
for (something in somethings) {
//Do something
}
Never try and throw code together, it won't work 99.9999% of the time. Focus, gather your thoughts and think what should happen and how do you achieve this.
Answer by Kavorka · Feb 02, 2014 at 08:03 PM
If this is what you want:
"Alternatively, I'd like to assign certain health to certain body part, and have it respond to rag doll physics without requiring the character to die. In other words, if the character gets shot in the head, I want his head to swing back, and snap back into place. Similarly, I would like that to happen to shoulders, arms, chest, and legs."
you should look at this AnimFollow.
AnimFollow makes a ragdoll follow an animation with some strength and lets you lower the strength for individual body parts so that they may be affected by physics. Then lerp the strength back to make it follow the animation again.
Answer by lelis718 · Nov 02, 2015 at 02:45 PM
Maybe this is what you are looking for
http://perttuh.blogspot.fi/2013/10/unity-mecanim-and-ragdolls.html
[+1] Thanks, a working sample is the easiest way to learn :)