This week was all about designing the basic elements of my character. This essentially meant modifying a correct jump height, creating a sprint mechanic, natural head movement while standing still (to simulate breathing), and a stamina bar. Some of these will be hard to put in a picture, as there are animations involved, but I’ll try my best.
My knowledge with coding in Unreal Engine 4 – well, it can be barely considered as coding since I just drag and drop things; its actually called blueprints – was very limited. My internship site was closed on Monday during Week 2, which is why I’ve just decided to combine Week 2 and Week 3 into one massive post about my character. I spent the first half of the week reading up on blueprints and how to create the different mechanics of my character. Then, I proceeded to start first with the sprinting mechanic. This turned out to be pretty simple. By establishing variables for Walk Speed and Sprint Speed, I was able to create a rather simple blueprint for that. My knowledge with coding in Unreal Engine 4 – well, it can be barely considered as coding since I just drag and drop things; its actually called blueprints – was very limited. My internship site was closed on Monday during Week 2, which is why I’ve just decided to combine Week 2 and Week 3 into one massive post about my character. I spent the first half of the week reading up on blueprints and how to create the different mechanics of my character. Then, I proceeded to start first with the sprinting mechanic. This turned out to be pretty simple. By establishing variables for Walk Speed and Sprint Speed, I was able to create a rather simple blueprint for that.
I mapped a function, Sprint, to the Left Shift key, which is default for sprint in most games. So when I held down Lshift, it would first check whether I can run (this is related to the stamina bar, which I’ll talk about in a bit). If I can run, then I’ll set the boolean “Is Running” to true, and therefore set the max walk speed (or maximum movement speed) to the sprint speed, which I established as 600. When I released Lshift, it would set “Is Running” to false and set the maximum movement speed back to the walking speed, which is 300.
Next, I needed to establish a stamina bar. Like all humans, my character will get tired after sprinting for a while. I needed to create a meter so the player could see how much stamina they had left. The idea was that while the player was moving and sprinting, the stamina bar would go down. However, if the player was simply walking or standing still, stamina would regenerate. As you can see below, this was a LOT more complicated to program compared to the sprint mechanic:
Essentially, I established an “event” that occurred: a few times every second, the game would repeatedly check to see if I was still sprinting. As you can see, my stamina bar was a value from 0 to 100, for simplicity’s sake. While sprinting, my stamina would decay as you can see in the blue box at the top (noted as Stamina Decay for clarity). Conversely, if I wasn’t sprinting, then my stamina would recover. In the latter half, It would check to see if my Stamina bar reached 0. If it did, it would prevent me from being able to run and it would set my movement back to the default walking speed.
In the bottom right, you can see that it would print a “string”, which was literally just a number that would show up on the top left of the game, so the player could see how much stamina they had left. Then, I created a little part of the blueprint where if the stamina went below 20, the player would start to breath heavily to let the player know their stamina was under 20%, even if they weren’t looking at the top left. To prevent infinite looping, I set a retriggerable delay so the player wouldn’t continuously pant like a dog.
I thought I was finished, so I tested it out. I quickly ran into an error which took me a whole day to try and fix. Under the original code I had, as long as I held Lshift, it would consider it sprinting and my stamina bar would go down. This meant that if I wasn’t moving and only holding down Lshift, my stamina would go down even though I was standing still. This was how I fixed it:
As you can see on the very left, I created a secondary branch in addition to the original branch (the second branch). I used a command GetVelocity and VectorLength to determine if the player was moving. If the value was greater than 0, then continue the function as normal. If it was false, then as you can see in the blueprint, the branch would directly connect to stamina recovery, so if the value was 0, stamina would simply regenerate.
This is what the stamina bar looks like in game, you can see the numbers on the top left. My stamina was regenerating in the screenshot, and as the stamina value updates, it pushes the old numbers down, so you can see my stamina value at the time of the screenshot was 19%.
Finally, I established a rhythmic breathing headbob for the character. This was a seperate blueprint class that didn’t contain any actual blueprints like the sprint and stamina bar mechanics, which were part of the character.
This proved to be the most difficult part, because in truth, I didn’t know what any of these values did. I looked online to see what these values did, but in the end, it honestly boiled down to a trial and error process where I just kinda changed one value after another to try and get the right combination. These values controlled how the camera was animated, and controlled how it moved around with the player. I set the oscillation duration to 0.25 sec so basically the animation would play 4 times every second. This alone took around three days, but it’s turned out very nicely and now my player has a nice head movement while standing still and while moving.
Since I’ve finished established velocity and jumping height values, I can now go forward with designing the mansion of my house and finding an appropriate avatar for my character, because right now, I’m just using the default mannequin.