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!
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.
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.
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:
- 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
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:
- 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
- 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
Here 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:
- 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.
Be a part of the Journey
Join my mailing list to receive the latest updates and stay in touch with me. I greatly appreciate every reader (^.^)b Or follow me on Twitter: @GamedevCala