UGameplayStatics::DeprojectScreenToWorld
#include "Kismet/GameplayStatics.h"
Access: public
Specifiers: staticBlueprintPure
Description
Converts a 2D screen-space pixel coordinate into a world-space position and direction ray. The standard starting point for mouse-to-world raycasts and click-to-move inputs.
Caveats & Gotchas
- • WorldPosition is on the camera near clip plane, not at any surface — to find a surface hit, pass WorldPosition and WorldDirection into a LineTraceSingleByChannel call.
- • Returns false if the player controller has no valid local player or the viewport is not initialized; always check the return value before using the outputs.
- • In split-screen, ScreenPosition must be in the player's sub-viewport coordinates; using full-screen pixel coordinates for a non-primary player gives incorrect results.
Signature
static ENGINE_API bool DeprojectScreenToWorld(APlayerController const* Player, const FVector2D& ScreenPosition, FVector& WorldPosition, FVector& WorldDirection); Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Player | APlayerController const* | The player controller whose camera view is used for deprojection. | — |
| ScreenPosition | const FVector2D& | 2D screen-space coordinate to deproject, in pixels. | — |
| WorldPosition | FVector& | Output: the world-space point on the near clip plane corresponding to the screen coordinate. | — |
| WorldDirection | FVector& | Output: normalized world-space direction from the camera through the given screen position. | — |
Return Type
bool Example
Raycast from mouse cursor into world C++
FVector WorldPos, WorldDir;
if (UGameplayStatics::DeprojectScreenToWorld(PlayerController, MousePos, WorldPos, WorldDir))
{
FHitResult Hit;
FVector End = WorldPos + WorldDir * 10000.f;
GetWorld()->LineTraceSingleByChannel(Hit, WorldPos, End, ECC_Visibility);
if (Hit.bBlockingHit)
{
MoveActorTo(Hit.ImpactPoint);
}
} Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?