Hello. I’m trying to make a line trace from a position on the screen, based on a widget, out into the 3d world. I’m having trouble figuring out how to do it. I’m wanting to draw a line from this one white dot, to an indefinite distance outwards.
I am using the thing called AimReticle for the center of the ray, but my code is mis-aligning more the further I move the reticle, what math or code should I use to fix this issue?
GameplayStatics library you can access in blueprint has a method for this (DeprojectScreenToWorld). My example gets the cursor position in 2D and deprojects it forward into 3D.
if (IsValid(PlayerController)) {
float OutX = 0.f;
float OutY = 0.f;
FVector OutDeprojectedPosition = FVector::ZeroVector;
FVector OutDeprojectedDirection = FVector::ZeroVector;
if (PlayerController->GetMousePosition(OutX, OutY)) {
// Get the cursor position and forward direction in world coordinates.
UGameplayStatics::DeprojectScreenToWorld(PlayerController, FVector2D(OutX, OutY), OutDeprojectedPosition, OutDeprojectedDirection);
}
}
What you are showing on the image is the center of the screen + the offset of the canvas slot position which I believe is not what you want, since you would only access 25% of the screen size. Can you test the GetPosition node as direct input for the Deproject node?
That worked! I thank you for your ingenuity! I may not have deciphered this on my own, though I feel at times the documentation for these things were more detailed.