Tutorial: Platformer Player Movement - CodingDino/Dino-Unity-Toolkit GitHub Wiki

This tutorial will walk you through adding platformer style movement to your player avatar. All you need to start is a sprite for your player avatar. If you don't want to create your own sprite, you can use the one in the tutorials folder of the toolkit.

Setup

First, drag your sprite into the scene. You should now have a GameObject in your scene with just a SpriteRenderer attached.

New Player

Now that we have our player, we need to think logically about what we want the player to do. For platformer movement, we need several things:

  • Our player should move left or right when we press a/d or left and right arrows
  • Our player should jump when we press space bar
  • Our player should fall downward if there is nothing under them
  • Our player should not be able to jump if they are not standing on the ground

So, let's tackle these one at a time.

Left and Right Movement

Our first order of business is to get our player moving left and right. Movement sounds like an Action. Remember that in the Dino Toolkit, Actions can't happen by themselves and have to be triggered by an Activator. In this case, the Activator is pressing a button - or to be more precise, pressing one of two buttons. With movement there is a positive and negative button, this is called an "Axis" in Unity. So the Activator we need is not the InputButtonActivator - it is instead the InputAxisActivator.

Click on your player, click on the "Add Component" button in the Inspector panel, and type in "InputAxisActivator" and add it. Your player should now have the new component.

InputAxisActivator

At the moment, it doesn't know what keys to pay attention to. Find the Axis Name field and set it to "Horizontal" - this is an axis set up by default in Unity to handle a/d and arrow key presses.

Axis Name

Right now there is nothing for this Activator to trigger. What we want is when the player presses a button on the axis, the player avatar will perform an Action - move left or right. For platformer movement, we will use velocity to control movement.

Let's add a SetVelocity2DAction component to our player avatar, in the same way we added the InputAxisActivator component above. This will automatically also add a RigidBody2D component to handle the player's physics, since our SetVelocity2DAction component requires it to work.

SetVelocity2DAction

If you pressed play right now, your player would start falling! We don't want that just yet (we'll tackle that later in the tutorial), so on the RigidBody2D component, change the Gravity Scale field to 0.

Gravity Zero

Let's also find the Speed field on our SetVelocity2DAction and set it to 10 - this will be a bit more obvious movement.

Speed

We now have an Action and an Activator, but they aren't hooked up! In your InputAxisActivator component, find the "OnAxisActive" Action list field. Add our SetVelocity2DAction to the list by clicking the plus button the bottom right of the action list, then dragging the Player GameObject in to the new slot.

InSlot

Finally, from the dropdown list, find the SetVelocity2DAction and open its sub-list. The action we want is "ActionSetXVelocity", because X is left to right. However, this action is in the list twice! It's really important in this case that we choose the one at the top, in the "Dynamic" section. Dynamic actions have information passed to them from the Activator. In this case, it will pas along which direction key we are pressing, left or right.

Dynamic Action

Time to test it! If you press play now, you should be able to move your player avatar left and right by using a/d or the arrow keys.

Platformer Movement Horizontal

Falling

Before we get jumping working, let's add falling back in. All you have to do is change the RigidBody2D component's Gravity Scale field back to 1.

Gravity 1

When you press play, your player avatar will fall out of the camera view.

Falling

That's not great - we can't see our character anymore! Let's add a simple platform in to catch them. You can use any sprite you want - in the tutorial files we have a simple pixel to use and stretch to cover most of the bottom of the screen. Just make sure the platform is big enough to walk around on.

Platform

If you press play right now, the player will fall right through this platform. That's because neither of them have Collider components. Without Colliders, Unity doesn't know what shapes these objects are and can't use collision detection to make them bounce off eachother.

On your new platform, add a new component - a BoxCollider2D. You can edit the collider so that it fits your platform by using the Edit Collider button.

Platform Collider

Add a Collider to your player as well. For players, the CapsuleCollider2D is a good default choice, but you can choose different Colliders based on the shape of your player. Again, make sure to edit your Collider so it matches the visual shape of your player.

Player Collider

Important Note: You must use 2D style colliders, or your physics won't work properly for 2D games!

Now if you press play, your player should be caught by the platform! However, you might notice your player easily falls over if you try to move...

Tip Over

This is easily fixed by adding a constraint on rotation to our RigidBody2D. This means the physics engine is not allowed to change the rotation of our player. Go to the RigidBody2D component on the player, expand the "Constraints" section, and check the "Z" box for "Freeze Rotation".

Freeze Rotation

Now if you press play, your player should fall and be caught by the platform!

Caught

Jumping

Finally, we can get to jumping. Just as with horizontal movement, jumping is done by using an Activator to trigger an Action. In this case, we don't have positive and negative buttons - just a single button, so we will be using an InputButtonActivator component to trigger our jump. Add that to the player now.

Just like with our InputAxisActivator, we need to tell our InputButtonActivator what button to use. For the Button field, add "Jump" as the button. By default, Unity has this button set up to be the space bar.

InputButtonActivator

Jumping works using forces, so the Action component we should use is the ApplyForce2DAction. Add this to your player.

ApplyForce2DAction

Now we just need to hook them up. Find the On Button Pressed Action list field on the player's InputButtonActivator component. Drag in the player to the slot (the action we want to trigger is on the player).

Jump Activator

Now select the ApplyForce2DAction and the ActionApplyYForce function. This time it doesn't need to be Dynamic, because our Activator doesn't need to send any extra information to the Action.

Jump Action Function

However, we get another field now after selecting our Action function - a number. This is how much force we should apply. This will control how high our player can jump. Let's set it to 300 for now - we can always change it later.

Jump Force

Now if you press play, you can jump!

Jump Works

Restrict Jumping

The only problem now is that you can jump infinitely! This isn't too hard to fix. Logically, what we want is to turn off the ability to jump when your feet aren't touching the ground, and turn it on again when they are touching the ground. Sounds like we need an Activator to sense touching the ground, and an Action to disable or enable a component!

First, let's add a new collision box to represent the player's feet. Create a new GameObject as a child of the player by right-clicking the player in the hierarchy and choosing "Create Empty". Name the new GameObject "Player-Feet".

On the new GameObject, add a BoxCollider2D component. Use the Edit Collider button to position the collider around the player's feet - it should extend a bit below their feet so it can sense the ground they are standing on. You should also change the collider to be a trigger by checking the "IsTrigger" box.

Feet

Now, add a new Activator component to the feet - the OnTrigger2DActivator. This Activator will trigger an action based on whether anything is within the trigger area defined by the attached Collider. Now all we need to do is make it turn on our jump ability when we touch the ground, and turn it off when we leave the ground!

Find the On Trigger Enter Action list field on the OnTrigger2DActivator. Create a new entry in the list and drag our main player GameObject in. Find the InputButtonActivator, which we use to trigger our jumps. Now, instead of finding an action function, we'll just use the built in Unity "enabled" function. Select this, and check the check box - this means that when something enters the feet Collider zone, we will enable the InputButtonActivator on the player.

Feet Trigger Enter

Finally, go down to the On Trigger Exit Action list field. Create a new entry and drag in the player. Find the same enabled function for the InputButtonActivator, but this time, uncheck the box. This will disable the InputButtonActivator when something leaves the feet Collider zone.

Feet Trigger Exit

Now if you press play, your player should only be able to jump if touching the ground!

Further Development

Your player works, but they aren't very polished. You might want to add a few things to make them seem more complete, such as:

  • Walking, jumping, and falling animations using an Animator component and an AnimationAction. You can set animation parameters based on the left/right buttons, and whether the player is touching the ground,
  • Jump sound effects using a AudioSource component and an AudioAction. You can trigger the AudioAction to play the audio when the player presses the jump button.
  • Flip the sprite left/right when changing directions using a FlipSpriteAction.

You can see the Example Platformer project to see how this might be done.