void Start() { _anim = GetComponent <Animator>(); _hipsTransform = _anim.GetBoneTransform(HumanBodyBones.Hips); _hipsTransformRigid = _hipsTransform.GetComponent <Rigidbody>(); _bzRagdollCharacter = GetComponent <IBzRagdollCharacter>(); //Get all the rigid bodies that belong to the ragdoll Rigidbody[] rigidBodies = GetComponentsInChildren <Rigidbody>(); foreach (Rigidbody rigid in rigidBodies) { if (rigid.transform == transform) { continue; } RigidComponent rigidCompontnt = new RigidComponent(rigid); _rigids.Add(rigidCompontnt); } // disable ragdoll by default ActivateRagdollParts(false); //Find all the transforms in the character, assuming that this script is attached to the root //For each of the transforms, create a BodyPart instance and store the transform foreach (var t in GetComponentsInChildren <Transform>()) { var trComp = new TransformComponent(t); _transforms.Add(trComp); } }
/// <summary> /// Prevents jittering (as a result of applying joint limits) of bone and smoothly translate rigid from animated mode to ragdoll /// </summary> /// <param name="rigid"></param> /// <returns></returns> static IEnumerator FixTransformAndEnableJoint(RigidComponent joint) { if (joint.Joint == null || !joint.Joint.autoConfigureConnectedAnchor) { yield break; } SoftJointLimit highTwistLimit = new SoftJointLimit(); SoftJointLimit lowTwistLimit = new SoftJointLimit(); SoftJointLimit swing1Limit = new SoftJointLimit(); SoftJointLimit swing2Limit = new SoftJointLimit(); SoftJointLimit curHighTwistLimit = highTwistLimit = joint.Joint.highTwistLimit; SoftJointLimit curLowTwistLimit = lowTwistLimit = joint.Joint.lowTwistLimit; SoftJointLimit curSwing1Limit = swing1Limit = joint.Joint.swing1Limit; SoftJointLimit curSwing2Limit = swing2Limit = joint.Joint.swing2Limit; float aTime = 0.3f; Vector3 startConPosition = joint.Joint.connectedBody.transform.InverseTransformVector( joint.Joint.transform.position - joint.Joint.connectedBody.transform.position); joint.Joint.autoConfigureConnectedAnchor = false; for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime) { Vector3 newConPosition = Vector3.Lerp(startConPosition, joint.ConnectedAnchorDefault, t); joint.Joint.connectedAnchor = newConPosition; curHighTwistLimit.limit = Mathf.Lerp(177, highTwistLimit.limit, t); curLowTwistLimit.limit = Mathf.Lerp(-177, lowTwistLimit.limit, t); curSwing1Limit.limit = Mathf.Lerp(177, swing1Limit.limit, t); curSwing2Limit.limit = Mathf.Lerp(177, swing2Limit.limit, t); joint.Joint.highTwistLimit = curHighTwistLimit; joint.Joint.lowTwistLimit = curLowTwistLimit; joint.Joint.swing1Limit = curSwing1Limit; joint.Joint.swing2Limit = curSwing2Limit; yield return(null); } joint.Joint.connectedAnchor = joint.ConnectedAnchorDefault; yield return(new WaitForFixedUpdate()); joint.Joint.autoConfigureConnectedAnchor = true; joint.Joint.highTwistLimit = highTwistLimit; joint.Joint.lowTwistLimit = lowTwistLimit; joint.Joint.swing1Limit = swing1Limit; joint.Joint.swing2Limit = swing2Limit; }