• No results found

Animating triggers – creating a death trap

In document Unity 5.x Animation Cookbook (Page 80-86)

In this recipe, we will animate a trigger position and use it to create a death trap. You can use Unity's Animation View to create various game play mechanisms with minimum scripting.

Getting ready

Before we start, you should prepare a death trap model. We are using a “press trap” as an example. It contains two identical moving elements. See the following screenshot:

The model is not skinned and has no rig; instead, the moving elements are separate objects.

We will animate them with Unity's Animation View. You can also use the example Unity project provided and go to the Chapter 02 Working with the animation

view\Recipe 06 Animating triggers - creating a death trap directory. You will find an Example.unity scene there. If you play the game, you can see the trap working. If you open the scene, you will find the Trap game object with the LeftTrap and RightTrap game objects as its children. Those child objects are the only ones animated in this example. They have trigger objects as their children. Those trigger objects kill the player when he enters them. All other child objects of the Trap game object are only decorations and are optional.

How to do it…

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

Create an empty object in the scene and name it Trap. It will be a parent object for 1. our moving parts.

Put your models into the scene and parent them to the Trap game object. The 2. models should have descriptive names, for instance, LeftTrap and RightTrap.

Add an Animator component to your Trap game object (the root object of our 3. death trap). Set the Update Mode to Animate Physics.

Go to Window | Animation and open the Animation View.

4.

Create a new Animation Clip by clicking on the Create button.

5.

Select the LeftTrap game object and make sure the record button is pressed (in 6. the upper-left corner of the Animation View).

Move the LeftTrap game object to the open position.

7.

Adjust the timeline a few seconds forward and move the LeftTrap game object to 8. the closed position.

Adjust the timeline a few seconds again and move the LeftTrap game object to 9. the open position again to create a looping animation.

Repeat steps 7-9 for the RightTrap game object.

10.

You should have a looping animation of the trap opening and closing. You can 11. 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.

Close the Animation View.

12.

We need to write a script for our character to be able to harm them with our 13. 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 {

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;

} } }

Select the LeftTrap game object and add a Box Collider component to it.

14.

Adjust the collider shape so that it will not cover the spikes section (we plan to 15. 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.

Add a Rigidbody component to the LeftTrap game object and set it to Is 16. Kinematic.

Right-click on the LeftTrap game object and choose 3D Object | Cube to add 17. a Cube child object to it. It will become our trigger.

Scale the Cube to cover the spikes with it. See the following screenshot:

18.

Remove the MeshRenderer and MeshFilter components from the Cube. Give 19. the Cube a descriptive name, for instance, LeftTrapTrigger.

Check the Is Trigger option in the Box Collider of the LeftTrapTrigger.

20.

Repeat steps 14-20 for the RightTrap.

21.

Now we need to write a script for the death trap itself. Create a new C# script and 22. 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 ();

} } }

Attach the DeathTrap.cs script to the LeftTrapTrigger game object and 23. the RightTrapTrigger game object.

Make sure your character has a Rigidbody component

24. (the ThirdPersonCharacter prefab from Unity's Standard assets will work).

Add the Character.cs script to your character.

25.

Create a blood particle effect and save it as a prefab. Name it BloodSplash.

26.

Assign the BloodSplash prefab to the Blood Effect field of the Character script 27. component in your character.

Play the game and enter the trap to see the effect. Your character should 28. disappear and the BloodSplash effect should be spawned.

How it works…

In this recipe, we attach triggers (game objects with Box Collider components set to Is Trigger) as child objects of our trap's moving parts. The OnTriggerEnter() method in our DeathTrap.cs script is called whenever an object with a Rigidbody component enters one of our trigger objects. It does not matter if the object or the trigger is moving. The animation of the moving parts of our trap also moves the child trigger objects. You can use it to create some really spectacular traps in your games.

There's more…

In our example, the character will die whenever it touches the spikes of our trap. We can add some more realism by using Animation Events to enable the triggers when the trap is closing and disable them when it opens. It would prevent us from killing the character when the trap is not closing on them. You can learn more about Animation Events in Chapter 6, Handling Combat.

Remember that you can also rotate the triggers, similar to how we did it in the Using the Hierarchy to animate local rotation – creating an orbiting planet recipe. Scaling the triggers is also possible.

You should not use this concept for moving normal colliders. Always try to use a Rigid body set to Is Kinematic when you want to move a collider (for optimization reasons).

Creating an elevator triggered by player

In document Unity 5.x Animation Cookbook (Page 80-86)