How to do it...

To animate the trigger position and create a death trap, follow these steps:

  1. Create an empty object in the scene and name it Trap. It will be a parent object for our moving parts.
  2. Put your models into the scene and parent them to the Trap game object. The models should have descriptive names, for instance, LeftTrap and RightTrap.
  3. Add an Animator component to your Trap game object (the root object of our death trap). Set the Update Mode to Animate Physics.
  4. Go to Window | Animation and open the Animation View.
  5. Create a new Animation Clip by clicking on the Create button.
  6. Select the LeftTrap game object and make sure the record button is pressed (in the upper-left corner of the Animation View).
  7. Move the LeftTrap game object to the open position.
  8. Adjust the timeline a few seconds forward and move the LeftTrap game object to the closed position.
  9. Adjust the timeline a few seconds again and move the LeftTrap game object to the open position again to create a looping animation.
  10. Repeat steps 7-9 for the RightTrap game object.
  11. You should have a looping animation of the trap opening and closing. You can adjust the animation until you're happy with it. You can add a pause in the open position to make it easier for the player to go through the trap without being killed.
  12. Close the Animation View.
  1. We need to write a script for our character to be able to harm them with our death trap. Create a new C# script and call it Character.cs. Write the following code:
     using UnityEngine; 
     using System.Collections; 
 
     public class Character : MonoBehaviour 
     { 
 
     /*We are going to store the reference to a blood effect prefab 
       in this variable*/ 
         public GameObject bloodEffect; 
 
       /*This variable is set to true when the character object was 
         already killed*/ 
         bool isKilled = false; 
 
         /*This function is called by the death 
         trap, when we enter it*/ 
         public void Kill () 
         { 
             /*If the character was already killed by the trap, 
             we don't want to do anything*/ 
             if (isKilled) { 
                 return; 
             } 
             /*If it was not killed, we set the isKilled 
             variable to true*/ 
             isKilled = true; 
 
            /*We check if the character has a Rigidbody component*/ 
             Rigidbody rb = GetComponent<Rigidbody> (); 
 
             if (rb != null) { 
 
                 /*If we find the component, we need to set it to 
                 kinematic to prevent our character from being 
                 launched in the air by the collision with our 
                 trap*/ 
                 rb.isKinematic = true; 
             } 
 
             /*Here we spawn a blood effect prefab stored in the 
             bloodEffect variable*/ 
         
            GameObject.Instantiate (bloodEffect, transform.position 
                     + Vector3.up * 2f, Quaternion.identity); 
 
             /*We are getting all the Renderer components of our 
             character*/ 
 
             Renderer[] r = GetComponentsInChildren<Renderer> (); 
 
             for (int i = 0; i < r.Length; i++) { 
                 /*We are turning all the renderers of, making the 
                 object dissapear*/ 
                 r [i].enabled = false; 
             } 
 
             /*We are also checking if our character uses our 
             SimpleMove script if so, we are turning it off to 
             prevent player from moving the character after death*/ 
         
             SimpleMove move = GetComponent<SimpleMove> (); 
 
             if (move != null) { 
                 move.enabled = false; 
             } 
 
         } 
     } 
  1. Select the LeftTrap game object and add a Box Collider component to it.
  2. Adjust the collider shape so that it will not cover the spikes section (we plan to add a trigger there). To do so, click on the Edit Shape button in the Box Collider component. Shapes handles will appear on every face of the Box Collider in the Scene View. You can click on the handle and move it to adjust the shape.
  3. Add a Rigidbody component to the LeftTrap game object and set it to Is Kinematic.
  4. Right-click on the LeftTrap game object and choose 3D Object | Cube to add a Cube child object to it. It will become our trigger.
  1. Scale the Cube to cover the spikes with it. See the following screenshot:
  1. Remove the MeshRenderer and MeshFilter components from the Cube. Give the Cube a descriptive name, for instance, LeftTrapTrigger.
  2. Check the Is Trigger option in the Box Collider of the LeftTrapTrigger.
  3. Repeat steps 14-20 for the RightTrap.
  4. Now we need to write a script for the death trap itself. Create a new C# script and call it DeathTrap.cs. Open the script and write the following code:
     using UnityEngine; 
     using System.Collections; 
 
     public class DeathTrap : MonoBehaviour 
     { 
 
         /*This function is called when a Rigidbody 
         enters the trigger object*/ 
         void OnTriggerEnter (Collider other) 
         { 
 
       /*We are checking if the object which entered the                      trigger has a Character script, if so we are calling the 
         Kill() method on it*/ 
         
             Character characterScript = 
             other.gameObject.GetComponent<Character> (); 
 
             if (characterScript != null) { 
                 characterScript.Kill (); 
             } 
         } 
     } 
  1. Attach the DeathTrap.cs script to the LeftTrapTrigger game object and the RightTrapTrigger game object.
  2. Make sure your character has a Rigidbody component (the ThirdPersonCharacter prefab from Unity's Standard assets will work).
  3. Add the Character.cs script to your character.
  4. Create a blood particle effect and save it as a prefab. Name it BloodSplash.
  5. Assign the BloodSplash prefab to the Blood Effect field of the Character script component in your character.
  6. Play the game and enter the trap to see the effect. Your character should disappear and the BloodSplash effect should be spawned.
..................Content has been hidden....................

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