2D Platformer with UE4 Part 2: Player Controller and Player Character

Posted By Calaelen in UE4 | 17 comments


This is an ongoing series of posts where I share my learning progress and resources while developing my first 2D Platformer with UE4 and Paper2D. Feel free to help me out if you know some tips & tricks. If you follow along, I would be happy to see your progress – get connected!

cala-ternity-moonwalk

Do the moonwalk!

Part 2: Player Controller and Player Character

In the first part, I set up the project and created a first test level with the help of a tile set and a tilemap. Now let’s have a look at two important parts of a game, the player controller and player pawn (character).

Where to begin

I watched these 9 videos from the 2D Sidescroller with Blueprints Tutorial by Epic Games. Zak Parrish guides you thru the process of creating your own 2D Character with Flipbook Animations and your own Animation State Machine. Take your time and follow along. That way I was able to understand the system and extend from it with my own ideas :)

After that you have a Blueman 2D character, which spawns at the player start position. Remember to check that the location is on the zero Y-axis. If you created the tilemap level from Part 1 with correct collisions, you should be able to jump and run around. Big step, be proud of yourself! :)

You should know more about these topics now:

  • Import Sprites and Spritesheets
  • Create Flipbooks
  • Setup Player Controller and Player Inputs
  • Animation State Machine

Create different Flipbook Animations for your states

The Blueman sprite is nice but not that thrilling after all. For my Jump and Run game I need some more animations / states anyway, so I built this little LPC character with a sprite sheet generator.

flipbook-animations-player-character

These are my various Flipbook animations right now. I have an animation for casting a spell (closing eyes and swinging arms) so I got away with some frames of it to mimic different states like Death, Flying, Hurt, Idle and Jump. You know: fake it, till you make it ;-)

Important tip: Set the pivot point to bottom center as this is the basis of a standing character. For the Hit animation I used a custom pivot point and lined the sprite up in relation to the smaller sizes of the other sprites. No hard science here, just some pixel pushing until it fits.

With these extra Flipbooks I extended the Animation State Machine from the Epic Tutorial Series (see above). I can switch to them via new bool variables the same way as with the idle and walking state.

ue4-blueprint-animation-state

Extending the Player Controller: Double Jump

Now for some fun. Double jumping! This is a basic version which could be tweaked a lot. But it is fun to double jump, that’s all that counts. The Blueprint:

ue4-blueprint-doublejump

What happens:

  • on InputAction event ‘Jump’ (project settings -> input)
  • check if we made less than 2 jumps
  • if so, add one to the jump counter and fire the Jump function from the player controller
  • when the character lands on some ground/platform, the event ‘OnLanded’ gets fired and the jump count will be set to 0 again

ue4-flipbook-hitenemy

Extend the Player Controller: Swing your weapon

This is split into two parts. The animation and actually dealing damage to the surrounding. Let’s have a look what happens when the Hit event gets fired:

ue4-blueprint-hit1

  • on pressing the hit key/button the system checks two states
  • if the character is getting hurt (getting pushed back) or if another swing with the sword is happening right now, I don’t want to start a new hit event to prevent a button smashing fire and forget playstyle
  • set the current state to “is Hitting” and update the animation within the Animation State Machine
  • 0.3sec delay before the next step, because the character animation swings back with the sword at first

ue4-blueprint-hit2

  • then the Collision Box in front of the Player Character gets activated to register potential targets
  • with the Melee Damage event (see below) we deal damage to enemies if they are within the Collision Box
  • after a short delay the ‘is Hitting’ state gets set back to inactive and the Collision Box is deactivated

redgirl-setupHere is a picture of the Player Controller Blueprint viewport where you can see the Collision Box in front of the sprite. On begin play this gets set to ‘no Collision’. The size is relative big as I don’t want to punish the player in hectic situations like jumping for and back while trying to hit some enemies.

In other games you would just spawn a projectile or cast a ray or something else. I thought of this way to simulate a melee attack. Maybe you know a better solution?

Extend the Player Controller: Deal Damage

The actual damage takes place in this custom event. While swinging the weapon, this function gets fired:

ue4-blueprint-applydamage

  • play a sword melee sound *woooosh*
  • Check and collect all actors which are overlapping the Sword Collision Box
  • for each of these actors check if they have the tag ‘Enemy’ and apply damage to them
  • the Player Controller is not responsible for what the actor does with this damage-message, we just tell them that they got hit with a base damage of 1
  • this could be extended with different weapons and more base damage and so on

Conclusion and next steps

Of course this is just a rough explanation of what is going on inside my Player Controller but I hope I can give you some hints and starting points to explore it on your own. The next post is about the game mechanics ‘getting damage’, ‘die’ and ‘heal’ with the help of Blueprint Components.

17 Comments

  1. Awesome stuff. Keep going, I’ll be waiting for every update :D

    Post a Reply
  2. When will be added the third part?

    Post a Reply
    • Thank you for asking. I hope to be able to write more about this topic very soon. If you like, please subscribe to my newsletter, so you will get a notice as soon as I release new posts :)

      Post a Reply
  3. Hi man,

    Nice work very helpful, can you please add some material examples like a change color of player, platforms etc?.. thanks….

    Post a Reply
    • Paper2D: These are sprites with just normal unlit material attached. If you like to change the color of something, you pixel yourself another sprite sheet ;-)

      Post a Reply
  4. Hello!

    I can’t make attack animation, I’m really confused how implement the attack animation into the Animation State Machine and the Event Graph. I have attack flipbook, but I don’t know how to add to the game. Can you make a video about this?

    Thanks! :)

    ( I’m hungarian, sorry for bad english :/ )

    Post a Reply
  5. I know this is very, very late. But I’m interested in how you implemented the sound. How did you did the sword attack part? And how would I implement that with say, jump noises or foot steps?

    Post a Reply
    • I am not sure if I understand your questions correctly. The implementation is written very detailed with screenshots in the last two paragraphs in this article. Just add the sounds at the correct moment with a play-node (not location aware / 3D – just simple play sound). Sounds for movement, jumps etc. could be added inside the update-animation graphs.

      Post a Reply
      • I’m not sure ho to define my .wav file as an audio component. Whenever I can get something to play (Besides music) it comes out as fuzz. I’ll just keep trying

        Post a Reply
  6. Hello, thak you so much for the post, all work fine for me except that my character gets hurt too when both characters collides

    what can i do ?

    Please i know this post has many years but i need a solution as soon as possible.

    Thank you so much

    Post a Reply
    • Have a look at the “Extend the Player Controller: Deal Damage” picture.
      There is a “actor has tag” node in it, which checks if the overlapping actors are tagged.

      You tag all your enemies but not the play controller. Now the damage will not be applied to your own character, as it doesn’t pass this gate.

      Post a Reply
  7. 2D paper. can you make tutorial for sound? for footsteps. I try but my sound does not work properly the sound is terrible plays the sound without interruption. or just give me a site with help, I can’t find

    Thank you so much

    Post a Reply
  8. i have a question how to do a sprint for 2d with a comand

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This