Skip to content

Instantly share code, notes, and snippets.

@sephirot47
Created February 25, 2021 10:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sephirot47/248dba7f922da30ce29d5b777d9fcc2a to your computer and use it in GitHub Desktop.
Save sephirot47/248dba7f922da30ce29d5b777d9fcc2a to your computer and use it in GitHub Desktop.
UE4 C++ Shoot through crosshair (or any other screen point)
const APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
// Get viewport size
int ViewportSizeX, ViewportSizeY;
PlayerController->GetViewportSize(ViewportSizeX, ViewportSizeY);
// Deproject screen point
FVector ShotStart, ShotDirection;
PlayerController->DeprojectScreenPositionToWorld(ViewportSizeX * 0.5f, ViewportSizeY * 0.5f, ShotStart, ShotDirection); // Assuming crosshair is in screen center
ShotStart += ShotDirection * 100.0f; // Offset a bit so that shot starts from gun end
const FVector ShotEnd = ShotStart + ShotDirection * 99999.9f;
DrawDebugLine(GetWorld(), ShotStart, ShotEnd, FColor::Green, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment