0
\$\begingroup\$

I have a ragdoll which performs dying quite well when it is initialized. But when the same prefab object is re-used by being pooled and recycled, it jerks violently when die. As if some forces were left on the rigidbodies when the mesh is disabled.

My code goes through the rigidbodies in the ragdoll and sets isKinematic when the enemy is alive and does his usual animations, but turns isKinematic false when the enemy dies so that ragdoll can take over. From there the pool's lifetime kicks in giving the corpse sometime to be a ragdoll, then the whole gameobject is disabled and pooled. When a new enemy is to generated the pool is asked for one and it is provided. The enemy enters the game normally and performs it's animations correctly. This is where the problem occurs, when the enemy is killed a second time it "jerks" upward as if collapsing in on itself.

The only thing I could think of it that there are some forces left on the mesh when disabled and they activate again when the ragdoll is enabled through death. So I went through all rigidbodies and do:

rigidbody.angularVelocity = Vector3.zero;
rigidbody.velocity = Vector3.zero;

But this doesn't also work, so I'm not sure if it's remnant forces or even if there's such a thing.

What else could be effecting the gameobject to cause a sudden jerk in the ragdoll, which supposedly doesn't have any forces applied on it's inception?

\$\endgroup\$
1
\$\begingroup\$

You do have to be careful to run

rigidbody.angularVelocity = Vector3.zero;
rigidbody.velocity = Vector3.zero;

when re-using RigidBodies from a pool. You haven't shared the full code you use to reset the velocity/angularVelocity for each Rigidbody, so it's possible there's a typo in your code. For example, this would not work as intended:

Rigidbody rigidbody = GetComponent<Rigidbody>();

Rigidbody[] childRigidBodies = GetComponentsInChildren<Rigidbody>();
foreach (var child in childRigidBodies) {
    rigidbody.velocity = Vector3.zero; //wrong reference!
    rigidbody.angularVelocity = Vector3.zero; //wrong reference!
}

Double-check that your code is actually reaching all of the Rigidbodies and resetting their velocities correctly. One easy way to check this is to apply some silly value that will be vary obvious:

Vector3 crazyVelocity = new Vector3(1000, 0, 0);
Rigidbody[] childRigidBodies = GetComponentsInChildren<Rigidbody>();
foreach (var child in childRigidBodies) {
    child.velocity = crazyVelocity;
    child.angularVelocity = Vector3.zero;
}

If your bodies go shooting off along the x-axis, you know that your code is reaching all of them. If not, then there's probably a mistake in your code.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Wider question here is that does forces remain on RigidBodies after disable/enable procedure? I tried this out quite a bit and I'm not sure. What happens is that animator positions the character's bones correctly after enabling, but they might go bad when the animator is disabled again. Still I don't know if this is the case, just guessing atp. \$\endgroup\$ – IndividualGames Jun 13 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.