public void DefaultInitialize() { RagdollBuilder builder = new RagdollBuilder(); builder.InitParams(transform); builder.SynchronousRagdoll(); builder.OnRagdollCreated = () => { RagdollBones = new Transform[(int)BodyParts.BODY_PART_COUNT]; RagdollBones[(int)BodyParts.Spine] = builder.pelvis; RagdollBones[(int)BodyParts.Chest] = builder.middleSpine; RagdollBones[(int)BodyParts.Head] = builder.head; RagdollBones[(int)BodyParts.LeftShoulder] = builder.leftArm; RagdollBones[(int)BodyParts.LeftElbow] = builder.leftElbow; RagdollBones[(int)BodyParts.RightShoulder] = builder.rightArm; RagdollBones[(int)BodyParts.RightElbow] = builder.rightElbow; RagdollBones[(int)BodyParts.LeftHip] = builder.leftHips; RagdollBones[(int)BodyParts.LeftKnee] = builder.leftKnee; RagdollBones[(int)BodyParts.RightHip] = builder.rightHips; RagdollBones[(int)BodyParts.RightKnee] = builder.rightKnee; builder.OnRagdollCreated = null; Initialize(); if (onCreateCompeleted != null) { onCreateCompeleted(); } }; builder.OnCreated(); }
public DeadBody DetachRagdoll(DamageInfo damageInfo) { if (null == m_ragdollBuilder) { return(null); } if (null == this.RootFrame) { return(null); } Transform ragdollTransform = this.RootFrame.transform; // remove colliders which are not part of ragdoll foreach (var c in m_ragdollBuilder.AdditionalColliders) { Object.Destroy(c); } m_ragdollBuilder.BuildBodies(); m_ragdollBuilder.BuildJoints(); m_ragdollBuilder.CalculateMass(); m_ragdollBuilder = null; ragdollTransform.SetParent(null); // setup rigid bodies foreach (var rb in ragdollTransform.GetComponentsInChildren <Rigidbody>()) { rb.drag = PedManager.Instance.ragdollDrag; if (PedManager.Instance.ragdollMaxDepenetrationVelocity >= 0) { rb.maxDepenetrationVelocity = PedManager.Instance.ragdollMaxDepenetrationVelocity; } rb.collisionDetectionMode = PedManager.Instance.ragdollCollisionDetectionMode; // add velocity to ragdoll based on current ped's velocity rb.velocity = m_ped.Velocity; } // apply force to a collider that was hit by a weapon if (damageInfo != null && damageInfo.raycastHitTransform != null && damageInfo.damageType == DamageType.Bullet) { var c = damageInfo.raycastHitTransform.GetComponent <Collider>(); if (c != null && c.attachedRigidbody != null) { c.attachedRigidbody.AddForceAtPosition( damageInfo.hitDirection * damageInfo.amount.SqrtOrZero() * PedManager.Instance.ragdollDamageForce, damageInfo.hitPoint, ForceMode.Impulse); } } // change layer ragdollTransform.gameObject.SetLayerRecursive(GameManager.DefaultLayerIndex); return(DeadBody.Create(ragdollTransform, m_ped)); }
public void ApplyForce(float forceMagtitude, Vector3 localForceDir, float mass = 20) { Animation animation = mGameObject.GetComponent <Animation>(); animation.enabled = false; RagdollBuilder ragdollBuilder = mGameObject.GetComponent <RagdollBuilder>(); if (ragdollBuilder == null) { ragdollBuilder = mGameObject.AddComponent <RagdollBuilder>(); } ragdollBuilder.totalMass = mass; ragdollBuilder.RagdollBuild(); Vector3 worldForceDir = mGameObject.transform.TransformDirection(localForceDir); Vector3 force = Vector3.Normalize(worldForceDir) * forceMagtitude; Transform[] parts; parts = ragdollBuilder.parts; foreach (Transform part in parts) { Rigidbody rigidbody; rigidbody = part.gameObject.GetComponent <Rigidbody>(); rigidbody.AddForce(force, ForceMode.Impulse); } }
void SetupRagdoll() { m_damageLevelPerBones.Clear(); RagdollBuilder rb = m_ragdollBuilder = new RagdollBuilder { pelvis = this.RootFrame.transform, leftHips = this.L_Thigh.transform, leftKnee = this.L_Calf.transform, leftFoot = this.L_Foot.transform, rightHips = this.R_Thigh.transform, rightKnee = this.R_Calf.transform, rightFoot = this.R_Foot.transform, leftArm = this.LeftUpperArm.transform, leftElbow = this.LeftForeArm.transform, rightArm = this.RightUpperArm.transform, rightElbow = this.RightForeArm.transform, middleSpine = this.UpperSpine.transform, head = this.Head.transform, jaw = this.Jaw.transform, }; rb.totalMass = PedManager.Instance.ragdollMass; rb.PrepareBones(); rb.OnWizardCreate(); // set layer this.RootFrame.gameObject.SetLayerRecursive(Ped.PedBoneLayerName); // assign damage level for bones Transform[] stomachAndChest = new Transform[] { this.RootFrame.transform, this.UpperSpine.transform }; Transform[] legsAndHands = new Transform[] { this.L_Thigh, this.R_Thigh, this.L_Calf, this.R_Calf, this.L_Foot, this.R_Foot, this.LeftUpperArm, this.RightUpperArm, this.LeftForeArm, this.RightForeArm }; Transform[] headAndJaw = new Transform[] { this.Head.transform, this.Jaw.transform }; foreach (var bone in stomachAndChest) { m_damageLevelPerBones[bone] = 2; } foreach (var bone in legsAndHands) { m_damageLevelPerBones[bone] = 1; } foreach (var bone in headAndJaw) { m_damageLevelPerBones[bone] = 3; } }