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);
}