Download

How can I make a Third Person Dynamic Crosshair

I am trying to figure out how I can make my crosshair move in the direction of where the gun is pointing. Currently I am just displaying the crosshair in the middle of the screen like so in my hud class:



const FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f);
	// offset by half the texture's dimensions so that the center of the texture aligns with the center of the Canvas
	const FVector2D CrosshairDrawPosition((Center.X - (CrosshairTex->GetSurfaceWidth() * 0.5)),
		(Center.Y - (CrosshairTex->GetSurfaceHeight() * 0.5f)));
	// draw the crosshair
	FCanvasTileItem TileItem(CrosshairDrawPosition, CrosshairTex->Resource, FLinearColor::White);
	AThirdPersonCharacter *Character = Cast <AThirdPersonCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
	TileItem.BlendMode = SE_BLEND_Translucent;
	if (Character)
	{
		if (Character->bIsAiming)
		{
			Canvas->DrawItem(TileItem);
		}
	}

I am calso storing the pitch and yaw used in my aim offset in my character class, so would I be able to make a vector out of those two floats and instead of making the “CrosshairDrawPosition” var a const, just assign it to that vector?

To better define by what i mean where the weapon is aiming is that I have a socket on each weapon labeled ‘MF’ where a projectile (bullet) is spawned into the world, so the aim direction would be the similar to the socket transform.(?)

I apologize if these seems trivial, I am not well versed in vectors and math involved in 3d space.

Any help or suggestions would be appreciated.

Okay, I came across this thread: ThirdPerson Crosshair Linked to Muzzle rather than center of screen? - Blueprint Visual Scripting - Unreal Engine Forums which worked perfect with some modifications! Now I just want to convert it to c++.