Create a 2D platformer with Unreal Engine 4. Part 1.5 - Jumping
The full-fledged “part 2” will be devoted to the combat system, but so far I have not brought it to the desired state, but I didn’t want to write anything at all for 2 weeks, so for now we’ll do a little expansion of our character’s abilities.
In the last article, we prepared a character and a simple scene, learned to move horizontally, it's time to learn, if not fly, then at least jump!
Minor changes in the project:
- Нашел новую раскадровку, переделал все спрайты Алукарда, теперь, как и положено, он в красном
- Добавил небольшие платформы, сделанные из текстуры пола для отработки прыжков
- Добавил фоновые картинки для атмосферы :)
First of all, we need to prepare animations. How to do this is described in part 1 , for convenience, I prefer to divide the jump into 2 conditional parts - jump and fall. To the top point in the air we play the first animation, after, respectively, the second. The fall animation can also be used when, in fact, falling from the edge of a cliff or other obstacles. We prepare Flipboocks, it should be noted that the playback speed and duration of the display of individual frames is selected individually.
Next, we proceed, in fact, to practice jumping. Go to our character’s Blueprint, add a jump event handler - InputActionJump. This is a simple keystroke handler, has two states - pressed and released. In principle, you can directly add the jump function to the keystroke, it will work, but we are trying to do well, right? Last time we started making a system for changing animations, it is easily expandable and can be very flexible, so we will use it.
To switch the animation, we need to know that a key has been pressed, so we add a boolean variable to the input and translate it into the state of truth. Thus, we will track the keystroke, after which there is a jump and zeroing (false) of our variable, with a delay - after all, the animation takes some time.
To release the key, add another Boolean variable - we will need it later. As a result, we get a simple algorithm:
Expanding our animation system. We have a new opportunity, so let's add a branch to the choice of animations: check if the character can jump (built-in function, thanks to the guys from Epic Games), if it can jump - we are behind the ground, go to an existing branch, if not - we are in air and animations need others.
Jump animation change branch
The logic here is simple: if we cannot jump, then we must determine for what reason - we are either jumping or falling. In the first case, we successively lose the jump / fall, in the second, only the fall.
Part 1 - Game Character, Movement