Game Development Reference
In-Depth Information
Now give that game a play and run off the platform. The player should now die
when it hits the death trigger and then respawn wherever the player respawn point
exists. Also, the camera should move as well, keeping its existing distance! Run off
those platforms all you want; you're going to come back every time. See? We keep
our promises.
Jump to it!
Running back and forth and falling off a platform to our death—now that is pretty
cool! But you know what it isn't? JUMPING.
Jumping is going to use a number of new properties. However, most of them aren't
anything that we haven't already worked with. Let's start by adjusting our states to
support jumping.
Jumping for fun (and profit)
First, we need to make it so that the player object can understand how to jump.
Open up the PlayerStateController script. We are going to add a condition to
check for jumping.
In the LateUpdate function of PlayerStateController , add the following code
after all of the left/right/idle checks that we have previously added:
float jump = Input.GetAxis("Jump");
if(jump > 0.0f)
{
if(onStateChange != null)
onStateChange(PlayerStateController.playerStates.jump);
}
We put this after the left/right/idle checks so that we can find the current movement
state. With this, we can determine what direction we want the player to jump in—or in
other words, we are allowing the player to jump left and right as well as straight up.
OK, so now the player can be told that they are jumping. Next, let's make it
actually jump! Head on over to PlayerStateListener and scroll on down to
onStateChange . In the newState switch, add a case for jumping and make it look
like the following:
case PlayerStateController.playerStates.jump:
if(playerHasLanded)
{
 
Search WWH ::




Custom Search