Created
February 25, 2021 10:13
-
-
Save sephirot47/248dba7f922da30ce29d5b777d9fcc2a to your computer and use it in GitHub Desktop.
UE4 C++ Shoot through crosshair (or any other screen point)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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