- Home /
From animation to ragdoll and back
Hello everyone,
following this tutorial I have an animated model that can turn into a ragdoll by setting isKinematic to false on all "bones" and disabling the animation. This works pretty much as intended. While in ragdoll state I apply some force to it, so that it moves away from its original position.
Now I want to switch back to the animated model. But if I just set isKinematic = true and reenable the animator, the model is "teleported" to its original position instead of standing where the ragdoll ended up lying.
I already tried to assign the animator to an empty object of which the model is a child instead of applying the animator on the model directly, but this doesn't change anything.
Edit:
I experimented a bit more. Even saving the new positions and applying them after turning on the animation does not help. The "bones" all lose their newly assigned position after the update call.
Answer by jacupiri · Sep 01, 2016 at 08:35 AM
DerDot, I found the answer for the same problem in the following link:
http://perttuh.blogspot.com.br/2013/10/unity-mecanim-and-ragdolls.html
It works just fine!
The only issue is with GameObjects that are child to your humanoid.
In my case, my character holds a sword, and falls to the ground still grasping it. But when he gets up (using any of both animations), the sword is out of his hand. It's still a child of the hand, but it is offset. And, I don't know why, the sword's collider is no longer a trigger.
By the way, Perrttu, your script is awesome! Congratulations! And thank you for sharing it!
I just would really like to know how to solve this small problem.
There is a solution pack for download in the link above.
DerDot, I found the whole solution for the ChildGameObjects. A guy called Wilian Silva pointed this out, at Perrttu's blog:
i found a solution just yesterday! Lets talk about the facts: this script recognizes every transform in the ragdoll as a bone, so you don't want your sword to be recognized as a bone. To fix that, you need to ignore all the transforms that are'nt a bone. This is what i did, i went to the Start function and made some changes:
I put a tag named "Bone" to every bone i wanted to do the transition, and this method checks if the transform has this tag, and then if it does, it adds this transform to the components.
_components = GetComponentsInChildren(typeof(Transform)); components = new Component[_components.Length];
for (int i = 0; i < _components.Length; i++) { if (_components[i].tag == "Bone") { components[i] = _components[i]; } }
Sorry for my bad english, and i hope it helps you! :D
I simplified Wilian's solution:
ins$$anonymous$$d of needing to tag all bones, I just put the condition of having a rigidbody (wich all ragdoll parts have). The Start() function of the "RagdollHelper" script (wich is the core of Perrttu's pack) ended up like this:
void Start ()
{
//Set all RigidBodies to kinematic so that they can be controlled with $$anonymous$$ecanim
//and there will be no glitches when transitioning to a ragdoll
set$$anonymous$$inematic(true);
//Find all the transforms in the character, assu$$anonymous$$g that this script is attached to the root
Component[] components=GetComponentsInChildren(typeof(Transform));
//For each of the transforms, create a BodyPart instance and store the transform
foreach (Component c in components)
{
if (c.GetComponent<Rigidbody> () != null)
{
BodyPart bodyPart = new BodyPart ();
bodyPart.transform = c as Transform;
bodyParts.Add (bodyPart);
}
}
//Store the Animator component
anim=GetComponent();
}
Thanks @jacupiri this sounds amazing! Sadly I won't have access to a PC for the next 3 to 5 weeks. I'll check it as soon as I can and will let you know.
Works exactly the way I want it, thanks a lot for the link!
Answer by The-Arc-Games · Jun 29, 2016 at 11:34 AM
There is basically just one solution to this need: dynamically create an animation/ animation state that your model uses to transition from the ragdoll position to the target animation position. You basically use the ragdoll rest position as the starting keyframe of a per-bone animation that has your desired length of time, and targets a last keyframe which corresponds with the first frame of your target animation (i.e. get up).
Achieving this involves considerable knowledge of the technicalities of animations and mecanim, but it requires relatively few steps.
Your answer
Follow this Question
Related Questions
ragdoll recover 5 Answers
problem with 2d ragdoll to animation 0 Answers
How to play transitioned animation at random time 1 Answer
Animation will not Return to Proper State 2 Answers
Mechainm Animation to Ragdoll 1 Answer