Game Development Reference
In-Depth Information
When working with a state system, it is best to include all states in
any function that works with the state system. All this does is keep
your code clean, and if you ever need to grow the functionality of
your states, you know that the basic state call is already handled
anywhere it might possibly be used.
And that's that! Jumping will no longer be locked up when you're holding down
the jump key. This completes the technical functionality of jumping!
Now your player can fall off the ledge after walking off of it. And with the power
of the state system, you won't be able to jump or move left or right in mid-air. Of
course, if you wanted to modify things so that the player can glide left and right,
the state system makes that easy to accomplish. Make it a challenge to yourself to
add gliding to the player's movements!
The most astute of you, while paying attention to the console, probably noticed an
error that keeps popping up:
SendMessagehitDeathTrigger has no receiver!
. What's
happening here is the
SendMessage
method in the death trigger is sending messages
to all colliders that hit it—which happens to include the
SceneryToggler
object. To
fix this, change how the death trigger is treated. Remove
DeathTriggerScript
from
the
Death Trigger
object. Then, go back into
PlayerColliderListener
and add the
following case to the
switch
condition inside
OnTriggerEnter2D
:
case "DeathTrigger":
// Player hit the death trigger - kill 'em!
targetStateListener.onStateChange(PlayerStateController.
playerStates.kill);
break;
So what was the point of creating the death trigger in a different way before?
The answer is that we could show you multiple ways to handle 2D collisions.
You're welcome!
Making the world bigger
Now that your player can jump around the world, let's give them a few more
platforms to jump on. First things first, let's make that platform standard. Let's create
a
Prefab
with it. In the
Project
tab, create a folder called
Prefabs
. Now, drag-and-drop
the
Platform
into the
Prefabs
folder to automatically turn it into a Prefab. Now you
can go ahead and place more platforms around the scene by dragging and dropping
the platform from the
Prefabs
folder into the scene, spacing them so the player can
jump between them. This is the power of Prefabs. Because now if we need to change
the platform, we simply change the one in the
Project
tab and
all
placed platforms in
the game world will automatically update with the changes.