Screen Position of Widget

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?

The base central position of the aim reticle, per it’s slow is center of the screen with an alignment of .5 for both X and Y.

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?

Try this:

Reticle

My Products

2 Likes

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.

Also, how did you make the vector 2d divided by a float instead of another vector 2d?

Disable it:

Ah! Thank you, two birds with one stone, eh?