How to do it...

To create a decapitation effect, follow these steps:

  1. Follow the Creating a humanoid ragdoll with the ragdoll wizard recipe to have a character with a working ragdoll and the HandleRagdoll.cs script (a script to turn the ragdoll on).
  2. Import the Neck and Head models and place them in the scene near the character.
  3. Create two empty game objects; name the first one HeadMarker and the second one NeckMarker.
  4. Parent the HeadMarker to the head bone and the NeckMarker to the neck bone. Make sure that the local position of HeadMarker and NeckMarker is 0.
  5. Parent the Head object to the HeadMarker and the Neck object to the NeckMarker. Zero out Head's and Neck's local rotation and position.
  1. Use the HeadMarker and NeckMarker to position the Head and the Neck to match the head and neck of the character as closely as possible. Remember that Head's and Neck's local position and rotation is 0, so when we position them by manipulating HeadMarker and NeckMarker, it will make is easier for us to spawn the Head and Neck later.
  2. Add a Sphere Collider and a Rigidbody component to the Head game object.
  3. Make a prefab from the Head game object and another one from the Neck game object.
  4. Delete Head and Neck from the scene, but make sure to leave HeadMarker and NeckMarker still parented to their corresponding bones in the character's rig.
  5. Create a new C# script and name it Dismemberment.cs. In this script's Update() function, we check whether the player presses the space bar and we call the void Decapitate() function if the wasSevered flag is false (you can really only decapitate someone once):
      if (Input.GetKeyDown(KeyCode.Space) && !wasSevered) 
      { 
          Decapitate(); 
      }     
  1. The void Decapitate() function does the rest. First it checks if the HandleRagdoll.cs script was found (the reference to it is set in the Start() function). If it was found, we enable the ragdoll with it and disable the script. Next we remove the Character JointRigidbody, and Collider components from the head bone (we are going to scale it to 0 because we don't want to bother with this joint anymore). Next we spawn the new, severed Head from a prefab stored in the public GameObject headPrefab variable. The Head is spawned with the HeadMarker position and rotation, a reference to which is stored in the public Transform headMarker variable. Then we get the Rigidbody component of the spawned Head and apply a random force and random torque to make it move. We use the public Vector3 variables to store the randomForce1randomForce2randomTorque1, and randomTorque2 values. We have a small helper function Vector3 RandomVector(Vector3 v1, Vector3 v2) that takes two vectors and returns a random vector build from those two parameters. Lastly, we scale the head bone to 0 (reference to it is stored in the public Transform head variable). Finally, we spawn the Neck prefab and parent it to the NeckMarker:
      wasSevered = true; 
      if (ragdollHandler != null) 
      { 
          ragdollHandler.EnableRagdoll(true); 
          ragdollHandler.enabled = false; 
      } 
      Joint headJoint = head.GetComponent<Joint>(); 
      if (headJoint != null) 
      { 
          Destroy(headJoint); 
      } 
      Collider headCollider = head.GetComponent<Collider>(); 
      if (headCollider != null) 
      { 
          Destroy(headCollider); 
      } 
      Rigidbody headRigidBody = head.GetComponent<Rigidbody>(); 
      if (headRigidBody != null) 
      { 
          Destroy(headRigidBody); 
      } 
      GameObject spawnedHead = 
      (GameObject)GameObject.Instantiate(headPrefab, 
      headMarker.position, headMarker.rotation); 
 
   Rigidbody spawnedHeadRB = spawnedHead.GetComponent<Rigidbody>(); 
      if (spawnedHeadRB != null) 
      { 
          spawnedHeadRB.AddForce(RandomVector(randomForce1, 
          randomForce2), ForceMode.Impulse); 
 
          spawnedHeadRB.AddTorque(RandomVector(randomTorque, 
          randomTorque2),ForceMode.Impulse); 
      } 
      head.localScale = Vector3.zero; 
 
      GameObject spawnedNeck = 
      (GameObject)GameObject.Instantiate(neckPrefab, 
      neckMarker.position, neckMarker.rotation); 
      spawnedNeck.transform.parent = neckMarker;     
  1. Add the script to the character.
  2. Play the game and press the space bar to see the effect.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.67.22