//similar to RagdollDeath, this function spawns body parts and sends the OnLimbDeath() Event void SpawnBodyPart(int limbID, Transform bodyPart, Vector3 attackPosition, Vector3 attackDirection, GameObject hitObj) { if (!usePooling) { Transform bodyPartClone = Instantiate(bodyPart, transform.position, transform.rotation) as Transform; GameObject limbClone = bodyPartClone.gameObject; RagdollLogic ragLogic = limbClone.GetComponent <RagdollLogic>(); ragLogic.parent = this.gameObject.GetComponent <CharacterSetup>(); ExecuteEvents.Execute <IBloodyMessEventHandler>(target, null, (x, y) => x.OnLimbDeath(limbID, limbClone, hitObj, attackPosition, attackDirection)); //same as above CopyOriginalTransforms(transform, bodyPart); } else { //spawn limbs with pooling //ExecuteEvents.Execute<IBloodyMessEventHandler>(target, null, (x,y)=>x.OnLimbDeath(limbID, limbClone, hitObj, attackPosition, attackDirection)); //same as above CopyOriginalTransforms(transform, bodyPart); } }
//used to explode the entire body, you must call from an outside script to activate public void ExplodeBody(Vector3 position, Vector3 direction) { if (target == null) { target = this.gameObject; } Rigidbody charRigid = gameObject.GetComponent <Rigidbody>(); charRigid.constraints = RigidbodyConstraints.FreezeAll; ExecuteEvents.Execute <IBloodyMessExplosionEventHandler>(target, null, (x, y) => x.OnExplosion(position, direction)); headOff = true; upperBodyOff = true; rightUpperOff = true; leftUpperOff = true; rightForArmOff = true; leftForArmOff = true; rightHandOff = true; rightLegOff = true; leftHandOff = true; leftLegOff = true; renderers.SetActive(false); skeleton.SetActive(false); StartCoroutine(RagdollDeath(position, direction)); foreach (Transform explosionPart in explosionParts) { Transform bodyPartClone = Instantiate(explosionPart, transform.position, transform.rotation) as Transform; GameObject limbClone = bodyPartClone.gameObject; RagdollLogic ragLogic = limbClone.GetComponent <RagdollLogic>(); ragLogic.parent = this.gameObject.GetComponent <CharacterSetup>(); Rigidbody[] rigids = bodyPartClone.GetComponentsInChildren <Rigidbody>(); foreach (Rigidbody rigid in rigids) { rigid.AddForce(Vector3.up * 10.0f, ForceMode.Impulse); rigid.AddForce(direction * 10.0f, ForceMode.Impulse); } } }
//handles ragdoll spawning upon character death and sends the OnDeath event public IEnumerator RagdollDeath(Vector3 position, Vector3 direction) { if (target == null) { target = this.gameObject; } yield return(new WaitForSeconds(ragdollWaitTime)); //here we replace the character with a ragdoll copy if (!advancedRagdoll) { if (!usePooling) { if (ragdoll) { Rigidbody rigid = gameObject.GetComponent <Rigidbody>(); Destroy(rigid); Collider col = gameObject.GetComponent <Collider>(); col.enabled = false; Transform ragdollClone = Instantiate(ragdoll, transform.position, transform.rotation) as Transform; RagdollLogic ragLogic = ragdollClone.GetComponent <RagdollLogic>(); ragLogic.parent = this.gameObject.GetComponent <CharacterSetup>(); ExecuteEvents.Execute <IBloodyMessEventHandler>(target, null, (x, y) => x.OnDeath(ragdollClone)); CopyOriginalTransforms(transform, ragdoll); //this will make sure the ragodoll comes in at the same "pose" as the previous model StartCoroutine(DestroyMainObject()); } } else { //enter spawn scripting from your pooling asset CopyOriginalTransforms(transform, ragdoll); } //effictively hide the character for now renderers.SetActive(false); skeleton.SetActive(false); //if we are not using pooling then we need to destroy the main object after a few seconds } else { Collider[] cols = gameObject.GetComponentsInChildren <Collider>(); foreach (Collider col in cols) { Rigidbody rigid = col.GetComponent <Rigidbody>(); if (col.enabled == true) { col.isTrigger = false; rigid.isKinematic = false; } else { rigid.isKinematic = false; rigid.useGravity = false; } } Rigidbody _rigid = gameObject.GetComponent <Rigidbody>(); _rigid.isKinematic = true; Collider _col = gameObject.GetComponent <Collider>(); _col.enabled = false; if (!usePooling) { StartCoroutine(DestroyMainObject()); } } }