Game Development Reference
In-Depth Information
Finally, go into the Animator component on the player. Disable Apply Root Motion
and enable Animate Physics by clicking on the checkboxes. This allows the physics
to have a little more control of the animations—something we don't really take
advantage of in this sample project. However, we still do this to prevent the engine
from making unwanted physics adjustments.
If you play the game now, you can move the player and fall off the edges of the
platform! And you can fall forever and ever, and you just keep falling—forever.
That's boring.
In the real world, you can't fall down a pit that's not more than a screen deep without
turning into a shower of dead pixels ( "Shower of Dead Pixels" just so happens to be
the name of my cover band). In this game, the player should cease to exist as well.
At least, they should cease to exist for a few short moments.
Falling fatally into death colliders
Like everything in this topic, there are many possible ways to set up death pits and
world boundaries. We are going to go over one option that allows you to specify a
world that is nonrectangular and allows death pits and boundaries to exist anywhere.
Let's start by adding an empty GameObject to the scene. This is going to be the core of
our death trigger. In fact, let's name it that—name it Death Trigger as it sounds nice
and ominous (and could also be a good backup name for my cover band).
Now, let's allow this Death Trigger GameObject to collide with the player. Add
a Box Collider 2D component, check the Is Trigger checkbox, and set its Size to
X: 20 , Y: 1 . Now, move its position to underneath the platform and set the Death
Trigger GameObject's Y position to -2.5 .
Let's add a new script. Call this one DeathTriggerScript and write its code
as follows:
using UnityEngine;
using System.Collections;
public class DeathTriggerScript : MonoBehaviour
{
void OnTriggerEnter2D( Collider2DcollidedObject )
{
collidedObject.SendMessage("hitDeathTrigger");
}
}
 
Search WWH ::




Custom Search