Categories
Uncategorised

GD2 Blog: Week 11

Unreal FPS part 1

As of the 13th of December, I’ve started following one of the official tutorials on using C++ with Unreal to help familiarize myself with navigating the documentation of the engine.

The tutorial I’ve been following is for how to implement mechanics for a First Person Shooter game.

You can find this here: https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/CPPTutorials/FirstPersonShooter/

First off, these are the inputs we will be adding to the project.

Figure 1
Figure 2

Movement is straightforward, as the values from the input scales are put towards movement input functions.

Figure 3

As for mouse movement, this is also fairly straightforward as we just set our mouse axis to the functions “AddControllerYawInput” and “AddControllerPitchInput”, which allow the player to rotate their view on the X and Y axis.

Figure 4

The tutorial also followed with us importing a mesh for the player and setting up a camera component, this is done by expanding our list of included files in the header file of the player character – this allows us to access more camera related functions.

“This will allow your code to access more camera-related functions, and will ultimately allow you to manipulate your camera placement.”

docs.unrealengine, 2021

Figure 5
Figure 6

Once we’ve added our new components from our header file, we’re able to modify them in our cpp file like so.

As seen in Figure 7, we create the camera component and then attach it to the capsule – we also allow the player pawn to control the rotation of our camera as well.

Included is also a mesh component which we added by using a USkeletalMeshComponent, we set this to be seen only by the owner and attach it to our camera so it follows where it goes.

Figure 7
Figure 8

This was the final result, we had a character that was able to move and look around including having a mesh and camera.

To add onto this tutorial, I decided to try and add one mechanic on my own – I tried to implement an input that would let the player crouch, which is not covered in the tutorial.

https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/GameFramework/UCharacterMovementComponent/

Using this documentation, I was able to gather a bunch of useful functions that worked in making the player crouch fairly easily.

Figure 9

Unreal has both a Crouch and Uncrouch function, which request the player to start or stop crouching respectively, they also call two other functions called “OnStartCrouch” and “OnEndCrouch” which I will be using.

Figure 10

Using a virtual void, I called these two useful functions which help detect when the player starts and stops crouching, so far these will be used to simply change the camera position to be lowered.

Figure 11

Using one of the variables, I’m able to set the height of the camera to be the half height of the capsule once crouching is initiated.

“HalfHeightAdjust: Difference between default collision half-height, and actual crouched capsule half-height.”

“ScaledHalfHeightAdjust: Difference after component scale is taken in to account.”

docs.unrealengine, 2021

Figure 12

When crouching is ended however, I just set the camera’s position to the BaseEyeHeight of the character’s capsule.

Figure 13

In-game, when the player pressed CTRL, then their capsule size would half and the camera drops down with it – the player would also be small enough to fit under objects they wouldn’t be able to pass through if they were standing.

I feel as if this was a good way to get myself more familiar with the Unreal Engine documentation, as it contains many helpful C++ tutorials as well as containing many references to functions used within them.

Bibliography

Docs.unrealengine.com. 2021. First Person Shooter Tutorial. [online] Available at: https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/CPPTutorials/FirstPersonShooter/ [Accessed 13 December 2021].

Docs.unrealengine.com. 2021. UCharacterMovementComponent. [online] Available at: https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/GameFramework/UCharacterMovementComponent/ [Accessed 13 December 2021].

Docs.unrealengine.com. 2021. ACharacter::OnStartCrouch. [online] Available at: https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/GameFramework/ACharacter/OnStartCrouch/ [Accessed 13 December 2021].

Leave a Reply

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