/* * ignore collisions with other physics bones on the same ragdoll */ public void IgnoreSelfCollisions(bool ignore) { if (CheckForErroredRagdoll("IgnoreSelfCollisions")) { return; } for (int i = 0; i < bonesCount; i++) { RagdollTransform boneA = allElements[i]; for (int x = i + 1; x < bonesCount; x++) { RagdollTransform boneB = allElements[x]; // dont handle connected joints, joint component already does if (boneB.joint && boneB.joint.connectedBody == boneA.rigidbody) { continue; } if (boneA.joint && boneA.joint.connectedBody == boneB.rigidbody) { continue; } Physics.IgnoreCollision(boneA.collider, boneB.collider, ignore); } } }
static Bounds GetBoxBounds(RagdollTransform bone, RagdollTransform topCutoff, RagdollTransform[] encapsulate, bool adjustMin) { Bounds bounds = new Bounds(); //encapsulate upper arms and upper legs positions for (int i = 0; i < 4; i++) { bounds.Encapsulate(bone.transform.InverseTransformPoint(encapsulate[i].transform.position)); } if (adjustMin) { Vector3 min = bounds.min; min.y = 0; bounds.min = min; } //adjust max bounds based on next bone Vector3 max = bounds.max; max.y = bone.transform.InverseTransformPoint(topCutoff.transform.position).y; bounds.max = max; return(bounds); }
static Collider AddBoxCollider(RagdollTransform bone, Bounds bounds) { BoxCollider box = bone.AddComponent <BoxCollider>(); box.center = bounds.center; box.size = bounds.size; return(box); }
public void LoadSnapshot(float snapshotBlend, bool useFollowTarget) { RagdollTransform element = useFollowTarget && followTarget != null ? followTarget : this; TeleportTo( Vector3.Lerp(element.transform.position, snapshotPosition, snapshotBlend), Quaternion.Slerp(element.GetRotation(), snapshotRotation, snapshotBlend) ); }
static void AddBreastColliders(Dictionary <HumanBodyBones, RagdollTransform> bones) { RagdollTransform[] encapsulate = new RagdollTransform[] { bones[HumanBodyBones.LeftUpperArm], bones[HumanBodyBones.RightUpperArm], bones[HumanBodyBones.LeftUpperLeg], bones[HumanBodyBones.RightUpperLeg], }; RagdollTransform hips = bones[HumanBodyBones.Hips]; RagdollTransform chest = bones[HumanBodyBones.Chest]; RagdollTransform head = bones[HumanBodyBones.Head]; hips.collider = AddBoxCollider(hips, GetBoxBounds(hips, chest, encapsulate, false)); chest.collider = AddBoxCollider(chest, GetBoxBounds(chest, head, encapsulate, true)); }
static void BuildCapsules(Dictionary <HumanBodyBones, RagdollTransform> bones) { foreach (var k in capsuleBones) { RagdollTransform bone = bones[k]; int direction = k.ToString().Contains("Arm") ? 0 : 1; float distance; if (upperCapsuleBones.Contains(k)) { distance = bone.transform.InverseTransformPoint(bones[Ragdoll.GetChildBone(k)].transform.position)[direction]; } else { Vector3 endPoint = (bone.transform.position - bones[Ragdoll.GetParentBone(k)].transform.position) + bone.transform.position; distance = bone.transform.InverseTransformPoint(endPoint)[direction]; if (bone.transform.GetComponentsInChildren(typeof(Transform)).Length > 1) { Bounds bounds = new Bounds(); foreach (Transform child in bone.transform.GetComponentsInChildren(typeof(Transform))) { bounds.Encapsulate(bone.transform.InverseTransformPoint(child.position)); } if (distance > 0) { distance = bounds.max[direction]; } else { distance = bounds.min[direction]; } } } CapsuleCollider collider = bone.AddComponent <CapsuleCollider>(); collider.direction = direction; Vector3 center = Vector3.zero; center[direction] = distance * 0.5F; collider.center = center; collider.height = Mathf.Abs(distance); bone.collider = collider; } }
public bool Transform2HumanBone(Transform transform, out RagdollTransform bone) { bone = null; if (CheckForErroredRagdoll("Transform2HumanBone")) { return(false); } for (int i = 0; i < bonesCount; i++) { if (allElements[i].transform == transform) { bone = allElements[i]; return(true); } } return(false); }
/* * Adjust Ragdoll component values per bone to reflect the supplied * Ragdoll profile (default profile if none is supplied) */ public static void UpdateBonesToProfileValues(Dictionary <HumanBodyBones, RagdollTransform> bones, RagdollProfile profile, float initialHeadOffsetFromChest) { if (bones == null) { return; } if (profile == null) { return; } Vector3 headOffset = profile.headOffset; //clamp head offset (values too high or too low become unstable for some reason) headOffset.y = Mathf.Clamp(headOffset.y, -initialHeadOffsetFromChest + .1f, 2); for (int i = 0; i < bonesCount; i++) { RagdollProfile.BoneProfile boneProfile = profile.boneData[humanBones[i]]; HumanBodyBones hBodyBone = humanBones[i]; RagdollTransform bone = bones[hBodyBone]; //set rigidbody values for bone UpdateRigidbodyToProfile(bone.rigidbody, boneProfile); //adjust collider values for bone UpdateColliderToProfile(hBodyBone, bone.collider, boneProfile, headOffset, initialHeadOffsetFromChest); //set joint values if (bone.joint) { UpdateJointToProfile(hBodyBone, bone.joint, boneProfile, headOffset); } } }
static void BuildJoints(Dictionary <HumanBodyBones, RagdollTransform> bones) { foreach (var k in bones.Keys) { if (k == HumanBodyBones.Hips) { continue; } RagdollTransform bone = bones[k]; bone.joint = bone.AddComponent <ConfigurableJoint>(); // Setup connection and axis //bone.joint.autoConfigureConnectedAnchor = false; // turn off to handle degenerated scenarios, like spawning inside geometry. bone.joint.enablePreprocessing = false; bone.joint.anchor = Vector3.zero; bone.joint.connectedBody = bones[Ragdoll.GetParentBone(k)].rigidbody; // Setup limits SoftJointLimit limit = new SoftJointLimit(); limit.contactDistance = 0; // default to zero, which automatically sets contact distance. limit.limit = 0; bone.joint.lowAngularXLimit = bone.joint.highAngularXLimit = bone.joint.angularYLimit = bone.joint.angularZLimit = limit; bone.joint.xMotion = bone.joint.yMotion = bone.joint.zMotion = ConfigurableJointMotion.Locked; bone.joint.angularXMotion = bone.joint.angularYMotion = bone.joint.angularZMotion = ConfigurableJointMotion.Limited; bone.joint.rotationDriveMode = RotationDriveMode.Slerp; bone.joint.projectionMode = JointProjectionMode.PositionAndRotation; } }
// ==================================== From here... public void SetFollowTarget(RagdollTransform followTarget) { this.followTarget = followTarget; }
/* * call to get the ragdoll bone references * * animator component must be humanoid */ public static bool BuildRagdollElements(Animator animator, out RagdollTransform[] allElements, out Dictionary <HumanBodyBones, RagdollTransform> boneElements) { allElements = null; boneElements = null; //check for null animator if (animator == null) { Debug.LogError("No animator found...(BuildRagdollFromPrebuilt"); return(false);// null; } List <RagdollTransform> allBonesList = new List <RagdollTransform>(); // instance ids of the bone transforms so we dont re-add them when checking all children bones below HashSet <int> usedPhysicsTransforms = new HashSet <int>(); //build bones list that use physics boneElements = new Dictionary <HumanBodyBones, RagdollTransform>(); for (int i = 0; i < Ragdoll.bonesCount; i++) { HumanBodyBones humanBodyBone = Ragdoll.humanBones[i]; Transform boneT = animator.GetBoneTransform(humanBodyBone); if (boneT == null) { Debug.LogError("Cant find physics bone: " + humanBodyBone + " on ragdoll: " + animator.name); boneElements = null; usedPhysicsTransforms = null; return(false); } usedPhysicsTransforms.Add(boneT.GetInstanceID()); RagdollTransform ragdollBone = new RagdollTransform(boneT, false, true, i == 0); boneElements.Add(humanBodyBone, ragdollBone); allBonesList.Add(ragdollBone); } //build other non physics bones //get all transform children of the hip bone Transform[] allChildren = allBonesList[0].transform.GetComponentsInChildren <Transform>(); for (int i = 0; i < allChildren.Length; i++) { Transform child = allChildren[i]; //if its not a physics bone if (!usedPhysicsTransforms.Contains(child.GetInstanceID())) { bool isPhysicsParent = child.GetComponentInChildren <Rigidbody>() != null; allBonesList.Add(new RagdollTransform(child, isPhysicsParent, false, false)); } } allElements = allBonesList.ToArray(); return(true); }