During our second Game Jam, our team made a space man zombie shooter hybrid. It was a 2D game, that didn't go as great as we hoped, but it was a good learning experience and taught me a lot about using Unreal Engine 5. I was one of two coders and the least experienced of the two. So I took on the task of setting up the game with our assets from our artist and implementing character movement as well as a crosshair that acted like a cursor, so that my other teammate could code a projectile to hit whatever the player is aiming at.
Here's the blueprints I made for both 2D movement and the crosshair cursor.
The movement blueprint can seem a little complicated at first and I did struggle to get the sprite to move left to right at first, as I had placed a top down camera with a 2D model. Below you can see my blueprint working within the play window. Both the movement and crosshair cursor are working which, admittedly (0.0) took me a lot longer than it should have!
The biggest issue I had was that the world and player model axis' got flipped. This is due to an error on my part and not being careful enough when setting up the sprite in game. The player model was imported incorrectly into the world, so it means that the movement worked oppositely to what I originally intended, as you can see below. In the future, I now know just how careful you have to be when setting up the player character, because this was a very frustrating experience until I figured it out and felt stupid. Going forwards, I will always ensure my model's axis and the world axis are correctly aligned to avoid this issue in the future!
You can see that I've set the desired world movement to 1 unit on the X axis for up/down movement and 1 unit on the Y axis for left/right movement.
This was all the code required to make the sprite move, but I extended the blueprint, so that the character would turn to face the direction they were moving, as you can see in the gif above.
As I had to animate the character by turning it into a paper flipbook, I needed the character to flip/rotate when moving in the opposite direction during it's left/right movements. For this I used an if statement (branch) to determine the sprite's rotation determined by the player's input. I set a boolean condition to detect movement inputs greater than 0. This is because I coded the movement as follows Right = 1.0 / Left = -1.0. This means that if the D key is pressed (assigned key I gave to the Right movement input) the player will move 1 unit to the right, and if the A key is pressed (assigned key I gave to the Left movement input) the player will move 1 unit in the opposite direction to the right input. This boolean condition was plugged into the branch (if statement) and if movement greater than 0 is detected then the code will follow the True branch and will maintain the character's right movement rotation. If an input less than 0 is detected, then I set the world rotation to flip the character's model on the Y axis 180 degrees, which essentially flips the 2D sprite to face the correct direction of movement. This is a useful thing to learn for 2D games, as it provides a better and smoother player experience, as it breaks immersion if the player character is constantly moving backwards.
In the GIF above, you can see my cursor I made. This was to be used to aim at the zombie characters, so that the spawned projectiles would have a path to follow. Making this was surprisingly easy. I just had to create a Widget blueprint and import the crosshair our artist made.
All I had to do here was create a begin play node and set the widget as the mouse cursor, which is the target for input. This means that the crosshair will appear as the mouse cursor when in the play window. This would be extremely useful when making on the rails shooters or games that require the use of a crosshair in the future.
Another neat trick I learned was how to create an 'animated' background. Our artist created a 2D moon style background and I imported that in as a texture. I created a material and added a rotator node to add the affect of the moon spinning for extra effect. I set the speed as 0.01 units as it was way too fast at 1 and could have potentially caused motion sickness for players. Originally I assumed UVs were involved with the world's lighting, but I actually found out that it's the images coordinate map. So in the rotator node has taken the x and y coordinates of the imported moon tile and has wrapped those points around around the background of our level and is rotating the image continuously to give it the movement effect. Knowing this would help a lot with side scrolling games like 'Flappy Bird'. It makes the game feel more alive than static and boring. Now that I also know that I can change the coordinates of the desired rotation points means that I can use this to make certain parts of my backgrounds/images move.
Did you like this post? Tell us