## Animated Spike Traps

• Game Programming 101

Learn the fundamentals of programming

• Beginner Scripting Examples

Get started by writing simple C# scripts in Unity

• Game Math

Learn the basics of game math by moving and rotating objects, applying force, and controlling time

• Controlling Animations

Learn how to use Mecanim with C# to play animations and control IK elements

• UI Scripting

Learn how to use C# for the UI system to control text and image components

### Interested in this Exercise?

Watch the course trailer to see what's inside, read the about tab, and then start learning on CG Cookie today.

Heya!

• Software: Unity 2018
• Difficulty:

## Watch out!

Spike traps are ubiquitous in many movies and games about exploring old ruins and in this exercise you'll learn how to put one together then simulate the players death upon being skewered. In this exercise you'll be a player carrying a large sphere as an added obstacle and they must travel to drop off this "treasure", but must avoid the deadly spike traps along the way. This spike trap looks harmless until the player walks over it and spikes shoot up, causing the player to go limp, instantaneously killing him and dropping his treasure in the process. Download the starting project files that have everything you need to get started.

## Submissions

Submit your exercise demonstrating what was shown in the video. You may add additional props like skeletons or even a new environment but the same spike trap and character must be used. Have fun!

## How To

If you have not completed this exercise do not proceed! This section is for those who have completed the exercise or have gotten stuck an need a little help completing the exercise.

The first rule was using IK with the player to hold the sphere. This was done in the same way as shown in this lesson Using Humanoid IK  I won't post the whole code in here as it'll be the same as shown in that video. This is what you should have from a visual stand point:

The head target is placed above the sphere and the hand targets are rotated and placed on the sphere itself. The sphere is also a child of the character so that it moves with the player.

The next rule was to enable the spikes to appear when the trigger detects the player entering. You'll need to have a trigger somewhere near the center of the spike trap itself. I used a cube with a box collider set to "is trigger":

Since the player already has ragdoll capabilities, that means his lower legs have capsule colliders and rigidbodies attached. Triggers require both a collider and rigidbody to detect, but we'll also be using a tag along with it. So I applied a tag of "Player" on the lower legs (with the rigidbody and collider) so that as he entered the trigger zone those colliders would be detected. Here is what the code looks like for this detection:

```void OnTriggerEnter (Collider col)
{
if(col.CompareTag("Player"))
{
anim.SetBool("trapEnabled", true);

}
}```

Here we're checking for an object that entered that also had a tag of "Player", if so, then set the "trapEnabled" bool of our spike trap Animator Controller to true, thus playing the animation of the spikes coming out.

Lastly for the ragdoll there are a few ways to approach this. You can use an Animation Event to fire off when the spike animation is at a certain frame, or you could time it using Invoke. The latter was the easier choice and provides more flexibility.

Along with the ragdoll we also needed to ensure that the sphere was no longer parented to the player, and when that happened we also need to turn off the IK weight to avoid having the player continue to try and hold the sphere. So we set the IK weight down to 0, turn off the Animator, enable the ragdoll by making the rigidbodies kinematic, and finally set the sphere's parent to null along with turning the rigidbody back to non-kinematic.

Phew, lots to do, but it all works together. Here is the code snippet for that portion:

```void OnTriggerEnter (Collider col)
{
if(col.CompareTag("Player"))
{
anim.SetBool("trapEnabled", true);
Invoke("RagdollDelay", 0.25f);

}
}
void RagdollDelay ()
{

playerIK.characterWeight = 0;
sphere.isKinematic = false;
sphere.transform.parent = null;
ragdoll.RagdollState(false);
anim.SetBool("trapEnabled", false);

}

```

This contains the OnTriggerEnter portion as we wanted to Invoke "RagdollDelay" by 0.25 seconds. We'll need a reference to the playerIK script along with a reference to the rigidbody of the sphere as well. At the end of the RagdollDelay we reset the "trapEnabled" bool so that the spikes retract back into the ground and wait for another unlucky foe to walk into it's treacherous path.

Take a look at the downloads section for the completed project files!

#### Rules of the Exercise

• 1
Use IK on player to control holding of sphere while walking
• 2
Play spike animations on player detection
• 3
Make play drop sphere when in a ragdoll state so that it rolls away