Dev Log#12 Creating a crosshair, a struct and learning about the HUD class in Unreal.
- camjhill737
- Apr 25, 2024
- 2 min read
So I've created a crosshair in unison between my combat component class and a new class that I created called EverzoneHUD I could have called it Game HUD but to avoid confusing naming conventions I replaced Game with the name of the title. This class is derived from HUD class provided by Unreal.
I created a struct to group my Crosshair variables as there are few different types of variables a struct will make it easier to call upon these variables.
How the crosshair functionality works:
Each part of the crosshair is a separate variable including the centre dot in the middle
The crosshair's Texture2D variables are being placed through the DrawTexture function found on the HUD class here https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Engine/GameFramework/AHUD/DrawTexture?application_version=5.0. In order to satisfy the parameters of this function I'm passing in each texture variable into a custom function called "DrawCrosshairs" alongside this is a Vector2D variable called ViewportCenter which stores the centre location of the screen. As well as a float called Spread which stores the distance between the center of the viewport and each crosshair variable and an FColor variable called crosshair colours.
DrawCrosshairs is where the size of the crosshairs can be customised, this calculation is stored in a Texture2D variable called "DrawTexturePoint". Ultimately what gets passed into the DrawTexture function is the crosshair's Texture2D variables, DrawTexturePoint's X and Y the width and height of crosshair variable, the colour of the crosshair, the UV variables are passed in as 0.f because we have taken care of the location of the crosshairs. likewise the UV width and height are passed in as 1.f.
Incorporating crosshairs with aiming:
Aiming is handled on my combat component so this is where my crosshairs are set.
There are many factors that will affect the size of my crosshairs such shooting, aiming, moving, falling and if the character is aiming at another player. I made use of the FInterpTo function on the FMath class https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Core/Math/FMath/FInterpTo?application_version=5.0. I use if checks with the conditions of if player is in air, is aiming, if the crosshairs are traced against another player and then use the interp function to change the the spread or size of the crosshairs and then in the else conditional statements I use another interp function to undo these effects to the crosshair. The results of these interp functions are stored inside a float factor for each respective check and then they're all added to the spread variable found on the HUD struct class which is the same variable that is responsible for drawing the distance between each crosshair on the HUD class.
Comentários