Download

[SOLVED] Mouse location to world space

Is there any way i can get my mouse position converted to world space? I have this Blueprint setup but its not working. I mean the XYZ is limited in 0-1 range.

&d=1400581785

The cursor position doesn’t exist in 3d space, so what I think you’ll want to do is get the position of the mouse cursor and then run a line trace from it to see what object is under the cursor, then return that position.

Well thats the problem. Get Mouse Position node from Player Controller is in 0-1 range. Theres another node called Convert Mouse Location to World Space but it doesnt seem to work either.

Convert Mouse Location to World Space does work, but you’ll need to do some tracing to get anything out of it. I believe one of the Blueprint examples shows this, but if not I think I have something working on one of my other machines, which I’ll have a look at later.

Projection and Deprojection is a bit fecked in BP right now, unfortunately.

You mean the Mouse Interface Example?

That will be a huge help Ambershee. Could you please share an example screenshot? :slight_smile:

Projection seems to do the job. I have the player name and objective markers projected onto the screen.

I just worked on this same thing.

The node Convert Mouse Location to World Space does what it is supposed to… but it takes its location from the “slice” in space that is the camera window.

It however also gives you a direction. So to get a point, take the “world direction” from the node, multiply it by… say 1000, then add that result to the “world location” from the node. You now have a point 1000 units from the point you clicked.

I have done this, in a bit more elaborate way to spawn some object at “water level” (z = 0) under the mouse cursor.

4 Likes

Thank you very very much Rasped! :D. This works like a charm.

No problem, I’m happy that it worked for you :slight_smile:

This could be converted to a snazy fast BPNode function right?
Something along the line of GetMousePositionInWorld with a vector3 out?

Sorry for the necro but thanks alot Rasped, this fixed an issue I had as well.

Nice! looking to do a set up similar to this.

Or, because handling gameplay mechanics like input through the HUD is pants-on-head-pencils-up-the-nose retarded, you could use this code in your own project to give you projection / deprojection functions you can use from anywhere:


FVector UMyGameplayStatics::Project(UObject* WorldContextObject, FVector Location)
{
	FPlane V(0, 0, 0, 0);
	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);
	ULocalPlayer* LocalPlayer = GEngine->GetLocalPlayerFromControllerId(World, 0);

	FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues(LocalPlayer->ViewportClient->Viewport, World->Scene, LocalPlayer->ViewportClient->EngineShowFlags).SetRealtimeUpdate(true));

	FVector OutViewLocation;
	FRotator OutViewRotation;
	FSceneView* SceneView = LocalPlayer->CalcSceneView(&ViewFamily, OutViewLocation, OutViewRotation, LocalPlayer->ViewportClient->Viewport);

	if (SceneView != NULL)
	{
		Location.DiagnosticCheckNaN();
		V = SceneView->Project(Location);
	}

	FVector2D ScreenDimensions;
	LocalPlayer->ViewportClient->GetViewportSize(ScreenDimensions);

	FVector resultVec(V);
	resultVec.X = (ScreenDimensions.X / 2.f) + (resultVec.X*(ScreenDimensions.X / 2.f));
	resultVec.Y *= -1.f * GProjectionSignY;
	resultVec.Y = (ScreenDimensions.Y / 2.f) + (resultVec.Y*(ScreenDimensions.Y / 2.f));

	// if behind the screen, clamp depth to the screen
	if (V.W <= 0.0f)
	{
		resultVec.Z = 0.0f;
	}
	return resultVec;
}

void UMyGameplayStatics::Deproject(UObject* WorldContextObject, FVector2D ScreenPos, /*out*/ FVector& WorldOrigin, /*out*/ FVector& WorldDirection)
{
	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);
	ULocalPlayer* LocalPlayer = GEngine->GetLocalPlayerFromControllerId(World, 0);

	FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues(
		LocalPlayer->ViewportClient->Viewport,
		World->Scene,
		LocalPlayer->ViewportClient->EngineShowFlags)
		.SetRealtimeUpdate(true));

	FVector OutViewLocation;
	FRotator OutViewRotation;
	FSceneView* SceneView = LocalPlayer->CalcSceneView(&ViewFamily, OutViewLocation, OutViewRotation, LocalPlayer->ViewportClient->Viewport);

	if (SceneView != NULL)
	{
		SceneView->DeprojectFVector2D(ScreenPos, /*out*/ WorldOrigin, /*out*/ WorldDirection);
	}
}

but how to do if z not 0, and z=1000?
Not correct if just “world direction” multiply by 1000.
Not understand!! :frowning:

im use:


how to fix that?

I know this is a ridiculously late reply, but thank you so much:D! This just solved the exact problem I had for the past hours and I knew there had to be a simple solution. Thank you once again!

Can somebody please point me in the right direction, when using this with UMG Widgets. Im not able to get the mouse position when drag & dropping.

For a better explanation. Im using kind of an Inventory with objects in it. When i drag the Inventory Item from the inventory into the world, i want to fire a LineTrace from that position & if it Hits an Actor start an Interace. All is working, when I Click on an Actor in World Space or drag an Object on it From the World. But when i Drag from the Widget, the is no mouse position to get.

Any help is very appriciated! c.

On the Widget you’re dragging, go to the Graph view and next to Functions, click from the Override option the dropdown item called On Mouse Button Down. Then also click On Drag Detected. In On Mouse Button Down, from Mouse Event, drag and create a node called Detect Drag If Pressed. Choose the Left Mouse Button (or whatever button you use to drag and drop!). Now connect the output to the return out.
Now in On Drag Detected, you can drag from the Pointer Event and Get Screen Space Position on the mouse cursor. With a Deproject Screen to World node you could get World Space if you need it. The result can be Promoted to a Variable and then passed on later for whatever other needs you may have for it.

Does this give you the desired functionality you wanted?

Works Great.

May be it’s help some one.

I use dotProduct insted. Direction and Z vector.
70.f is the height from the ground (z = 70.f)


cosDir = FVector::DotProduct(Direction, FVector(0, 0, 1));
SpawnLocation = Location + Direction * (Location.Z - 70.f) / cosDir * -1;

Sorry that isn’t blueprint.

So obviously I’m almost 4 years late to this party but I’m doing almost exactly what you are saying but getting the PointerEvent from OnDragCanceled, then using GetLastScreenSpacePosition and feeding it into DeprojectScreentoWorld.

This works perfectly in full screen but if in a windowed mode, like the editor, the trace seems to hit bottom-right to where it should do and not in a consistent offset it seems to be warped.

If i multiply the result of GetLastScreenSpacePosition by GetViewPortSize / GetScreenResolution before plugging it into DeprojectScreentoWorld the offset is different, the trace hitting closer to the center of the viewport than where I stopped dragging, no matter what side of the screen it is dropped. Anyone have any idea what the problem maybe?