Skip to content

Instantly share code, notes, and snippets.

@embiem
Created June 24, 2016 08:16
Show Gist options
  • Save embiem/d110bc0383c191aae39742b5fe80aa72 to your computer and use it in GitHub Desktop.
Save embiem/d110bc0383c191aae39742b5fe80aa72 to your computer and use it in GitHub Desktop.
bool GetSightRayHitLocation(FVector& HitLocation) const
{
// Find crosshair position in pixel coordinates
int32 ViewportSizeX, ViewportSizeY;
GetViewportSize(ViewportSizeX, ViewportSizeY);
FVector2D ScreenLocation = FVector2D(ViewportSizeX * CrossHairXLocation, ViewportSizeY * CrossHairYLocation);
// "De-project" the screen position of the crosshair to a world direction
FVector LookDirection, LookStartLocation;
if (DeprojectScreenPositionToWorld(ScreenLocation.X, ScreenLocation.Y, LookStartLocation, LookDirection))
{
// Line-trace along that look direction, and see what we hit
FHitResult HitResult;
if (GetWorld()->LineTraceSingleByChannel(
HitResult,
LookStartLocation,
(LookStartLocation + LookDirection * LineTraceRange),
ECollisionChannel::ECC_Visibility))
{
HitLocation = HitResult.Location;
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment