- Home /
How do I disable / enable a character joint?
I am trying to go from animation, to ragdoll, then back to animation. I can do this if I can enable / disable character joints. How can I accomplish this? I don't see a little check box.
disable it via script on a collision or when you want the ragdoll effect to happen. for example for something like a fallout type of death just write a script which says when health reaches zero disable iskinematic on all rigidbodys with joints.
I am saying that when is kinematic = true, the body doesn't fall, but the joints still react and dangle. Edit: I put it in every body and now it works! So I have to put it in every bone or is there a possible way around it?
every rigidbody with a joint connected to the body needs to have iskinematic set to true, you could make an editor script to do this for you but would be pointless unless you were always switching between them.
Answer by Panzermjau · Dec 21, 2014 at 10:05 AM
From what I've found the joint still applies its constraint even if rigidbody.isKinematic is set to true.
I solved it by destroying the joints on startup, then recreating them to enable the ragdoll. Not very elegant, but it seems to work ok.
public class RagdollEnabler : MonoBehaviour {
private List<CharacterJointDisabler> _jointDisablers;
private void Awake ()
{
CharacterJoint[] characterJoints = transform.GetComponentsInChildren<CharacterJoint>();
_jointDisablers = new List<CharacterJointDisabler>();
for(int i=0; i<characterJoints.Length; ++i)
{
characterJoints[i].collider.enabled = false;
characterJoints[i].rigidbody.isKinematic = true;
CharacterJointDisabler disabler = characterJoints[i].gameObject.AddComponent<CharacterJointDisabler>();
disabler.CopyValuesAndDestroyJoint(characterJoints[i]);
_jointDisablers.Add(disabler);
}
}
public void ActivateRagdoll ()
{
rigidbody.constraints = RigidbodyConstraints.None;
for(int i=0; i<_jointDisablers.Count; ++i)
{
_jointDisablers[i].collider.enabled = true;
_jointDisablers[i].rigidbody.isKinematic = false;
_jointDisablers[i].CreateJointAndDestoryThis();
}
}
}
public class CharacterJointDisabler : MonoBehaviour
{
private Rigidbody ConnectedBody;
private Vector3 Anchor;
private Vector3 Axis;
private bool AutoConfigureConnectedAnchor;
private Vector3 ConnectedAnchor;
private Vector3 SwingAxis;
private SoftJointLimit LowTwistLimit;
private SoftJointLimit HighTwistLimit;
private SoftJointLimit Swing1Limit;
private SoftJointLimit Swing2Limit;
private float BreakForce;
private float BreakTorque;
private bool EnableCollision;
public void CopyValuesAndDestroyJoint(CharacterJoint characterJoint)
{
ConnectedBody = characterJoint.connectedBody;
Anchor = characterJoint.anchor;
Axis = characterJoint.axis;
AutoConfigureConnectedAnchor = characterJoint.autoConfigureConnectedAnchor;
ConnectedAnchor = characterJoint.connectedAnchor;
SwingAxis = characterJoint.swingAxis;
LowTwistLimit = characterJoint.lowTwistLimit;
HighTwistLimit = characterJoint.highTwistLimit;
Swing1Limit = characterJoint.swing1Limit;
Swing2Limit = characterJoint.swing2Limit;
BreakForce = characterJoint.breakForce;
BreakTorque = characterJoint.breakTorque;
EnableCollision = characterJoint.enableCollision;
Destroy (characterJoint);
}
public void CreateJointAndDestoryThis()
{
CharacterJoint characterJoint = gameObject.AddComponent<CharacterJoint>();
characterJoint.connectedBody = ConnectedBody;
characterJoint.anchor = Anchor;
characterJoint.axis = Axis;
characterJoint.autoConfigureConnectedAnchor = AutoConfigureConnectedAnchor;
characterJoint.connectedAnchor = ConnectedAnchor;
characterJoint.swingAxis = SwingAxis;
characterJoint.lowTwistLimit = LowTwistLimit;
characterJoint.highTwistLimit = HighTwistLimit;
characterJoint.swing1Limit = Swing1Limit;
characterJoint.swing2Limit = Swing2Limit;
characterJoint.breakForce = BreakForce;
characterJoint.breakTorque = BreakTorque;
characterJoint.enableCollision = EnableCollision;
Destroy(this);
}
}
Answer by aldonaletto · Sep 23, 2012 at 01:28 AM
You can use a simple function to enable/disable the rigidbodies:
var rigids: Rigidbody[];
function Start(){ rigids = GetComponentsInChildren.< Rigidbody>(); }
// make all rigidbodies kinematic (true) or not (false):
function MakeKinematic(onOff: boolean){ for (var rb: Rigidbody in rigids){ rb.isKinematic = onOff; } }
Use
MakeKinematic(false) to enable the ragdoll, and
MakeKinematic(true) to disable it.
The character wont fall or react to forces now though. How can I get around that?
if you mean the character just stays in mid air when u hit play this is because you need to have one rigidbody set to false on iskinematic the parent object of the character or main root bone
I do that and it still floats because there's a lot of other rigid bodies in there too.
should be on the main root bone, their shouldn't be any other rigid bodies on a parent except for its children. their shouldnt be joints either on this one, only joints attached to it
try attaching some cubes together with joints and have the main one's iskinematic set to false whilst the rest are set to true
Answer by TheHgang · Jun 07, 2020 at 10:06 AM
Plz check Rigidbody iskinematic property and save urself lot of trouble