DeprojectScreenPositionToWorld not working as expected

My camera follows the character in thirdperson mode.

The red box / crosshair is a ui element and is in the middle of the screen

What I expect when I shoot is that there will be two debug arrows (purple and blue) pointing to the target so towards the red crosshair. This is my code:

// Using DeprojectScreenPositionToWorld
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
PlayerController->DeprojectScreenPositionToWorld(0.0f, 0.0f, WorldLocation, WorldDirection);
WorldDirection.Normalize();
const FVector End = WorldLocation + (WorldDirection * 1000.0f);
DrawDebugDirectionalArrow(GetWorld(), WorldLocation, End, 30.0f, FColor::Purple, true, 3.0f, 0, 3.0f);

// Using Camera location and rotation
const FVector CamLocation = CameraComponent->GetComponentLocation();
FVector CamDirection = CameraComponent->GetComponentRotation().Vector();
CamDirection.Normalize();
FVector CamEnd = CamLocation + (CamDirection * 1000.0f);
DrawDebugDirectionalArrow(GetWorld(), CamLocation, CamEnd, 30.0f, FColor::Blue, true, 3.0f, 0, 3.0f);

However, the result is that the arrow (purple) which was determined via DeprojectScreenPositionToWorld points somewhere completely else (this is after I turned my camera, so you can see the arrows):

The blue arrow seems to be right, but I don’t get what I’m doing wrong that the purple arrow doesn’t overlap the blue one. Can you help me see what I’m doing wrong with DeprojectScreenPositionToWorld? I think I’m missing something here.

The position (0,0) on screen is top left corner, not center.
You can use GetViewportSize and divide by 2 to get screen center.

1 Like

Awesome, thank you for your help @Chatouille, it worked!
The thing that threw me off was that it also didn’t work when I used DeprojectMousePositionToWorld. But now it all makes sense.