DebugLine does not pass through the center of the Crosshair?

I have first person shooter game with crosshair in the middle of screen.

The code below gives me the direction with inaccurate results

The DrawDebugLine does not move at the middle of crosshair ?

PullTrigger Function contains the code…

void AWeapon::PullTrigger() 
{

	APawn* OwnerPawn = Cast<APawn>(GetOwner());
	if (!OwnerPawn) return;

	AController* Controller = OwnerPawn->GetController();
	if (!Controller) return;

	APlayerController* PlayerController = Cast<APlayerController>(Controller);
	if (!PlayerController) return;

/**************** Getting Direction Area ********************/
	int32 OUT SizeX;
	int32 OUT SizeY;
	PlayerController->GetViewportSize(SizeX, SizeY);

	float XPos = SizeX * 0.5f;
	float YPos = SizeY * 0.5f;
	FVector2D ScreenPosition(XPos, YPos);

	FVector OUT WorldLocation;
	FVector OUT WorldDirection;
	PlayerController->DeprojectScreenPositionToWorld(ScreenPosition.X, ScreenPosition.Y, WorldLocation, WorldDirection);

/******************End Getting Direction Area********************/
	
	FVector StartLocation = GetActorLocation();
	FVector EndLocation = StartLocation + (WorldDirection * BulletMaxRange);

	DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor::Yellow, true, 0.05, 0, 1);
}

You’re using the actor’s location here rather than the location you got from GetPlayerViewPoint.

Thanks Mr @DanM for your response…
Look at my code, There is no problems when using GetPlayerViewPoint, and my code is not based on GetPlayerViewPoint now.

My code is below and you can see the problem of inaccurate DebugLine Direction in first post video above.

int32 OUT SizeX;
int32 OUT SizeY;
PlayerController->GetViewportSize(SizeX, SizeY);

float XPos = SizeX * 0.5f;
float YPos = SizeY * 0.5f;
FVector2D ScreenPosition(XPos, YPos);

FVector OUT WorldLocation;
FVector OUT WorldDirection;
PlayerController->DeprojectScreenPositionToWorld(ScreenPosition.X, ScreenPosition.Y, WorldLocation, WorldDirection);

FVector StartLocation = GetActorLocation();
FVector EndLocation = StartLocation + (WorldDirection * BulletMaxRange);

Sorry, I read it the other way around. It’s still the same issue though.

This is the problem, you’re using the actors location instead of the one you got from DeprojectScreenPositionToWorld

FVector StartLocation = WorldLocation;
FVector EndLocation = StartLocation + (WorldDirection * BulletMaxRange);

Still problem existing

I copied the code you provided and that’s the only change I made and it worked as expected.

Privacy & Terms