- Home /
My flipped character ragdoll has wrong hingejoint anchor positions
I'm working on an old endless zombie-killing sidescrolling runner-esque game. I did not start this project back in the day.
Dynamic Ragdolls
I recently added ragdolls to the game. By ragdolls I mean a collection of rigidbodies which are connected to each other through hingejoints. Since the zombies are basically animated boxes with 1 rigidbody each, my approach was to create the ragdoll 'dynamically', so I turn an animated box into a 'ragdoll'.
Flipping
Ragdolls work perfectly fine. Untill you flip the character. The game flips characters by inverting the x-axis. I also tried 'flipping' the character by rotating him around the Y-axis, but the ragdollifying results were the same.
I think the solution is either flipping zombies in another fashion, or somehow, magically fixing the anchor points when applying the hingejoints.
Code
The entire code for the zombie ragdoller-class is here: http://pastebin.com/99ZHWetZ
Here's a snippet of the HingeJoint2D-adding code:
private void ApplyHingeJoints()
{
Rigidbody2D rb2d_torso = Torso.GetComponent<Rigidbody2D>();
HingeJoint2D leftarm_joint = LeftArm.AddComponent<HingeJoint2D>();
HingeJoint2D rightarm_joint = RightArm.AddComponent<HingeJoint2D>();
leftarm_joint.connectedBody = rb2d_torso;
rightarm_joint.connectedBody = rb2d_torso;
HingeJoint2D lefthand_joint = LeftHand.AddComponent<HingeJoint2D>();
HingeJoint2D righthand_joint = RightHand.AddComponent<HingeJoint2D>();
lefthand_joint.connectedBody = LeftArm.GetComponent<Rigidbody2D>();
righthand_joint.connectedBody = RightArm.GetComponent<Rigidbody2D>();
JointAngleLimits2D handAngleLimits = new JointAngleLimits2D();
handAngleLimits.min = -30;
handAngleLimits.max = 30;
lefthand_joint.limits = righthand_joint.limits = handAngleLimits;
lefthand_joint.useLimits = righthand_joint.useLimits = true;
HingeJoint2D leftleg_joint = LeftLeg.AddComponent<HingeJoint2D>();
HingeJoint2D rightleg_joint = RightLeg.AddComponent<HingeJoint2D>();
leftleg_joint.connectedBody = rightleg_joint.connectedBody = rb2d_torso;
JointAngleLimits2D legAngleLimits = new JointAngleLimits2D();
handAngleLimits.min = 0;
handAngleLimits.max = 200;
leftleg_joint.limits = righthand_joint.limits = legAngleLimits;
leftleg_joint.useLimits = rightleg_joint.useLimits = true;
rightarm_joint.connectedAnchor = new Vector2(-0.15f, 4.15f);
leftarm_joint.connectedAnchor = new Vector2(-0.9f, 4f);
lefthand_joint.connectedAnchor = righthand_joint.connectedAnchor = new Vector2(-0.15f, -4.2f);
}
The flipping-code located in another class looks like this:
void Flip()
{
// Multiply the player's x local scale by -1
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
//transform.Rotate(new Vector3(0, 180, 0));
// Invert the rotation of the particle effect
bloodSplatter.startRotation *= -1;
}
Here's the unfortunate result from killing a flipped zombie. Red lines are arms, purple are hands, blue is torso and green are legs. The hands are supposed to be connected to the arms and the arms are supposed to be connected to the torso.
If you need anything, please just comment. Thanks in advance.
I've been having the same exact issue... was hoping there was an in-editor approach around this, but still have yet to find anything.