Convert InputTouch Location to World Location

Hi all.

I’m trying to get x and y coordinates from InputTouch and translate them to a world location. I believe this is similar to converting from a screen point but i can’t find any nodes to assist with this in BP.

Does any one have any suggestions?

What you want to do is convert the screen location into a world location on the front plane of your current camera view. Then you find the direction vector from the camera position to this plane position, and create a ray. You take this ray and intersect it with the world. Usually, you just want to grab the nearest object you intersect with.

Fortunately, this is pretty easy to do and all of its mostly done for you.

In C++ code, you’d want to do this in the player controller, like so:

FHitResult hit;
if (Controller->GetHitResultAtScreenPosition(ScreenPosition, COLLISION_TRACECHANNEL, true, hit))
{
	FVector HitPos = hit.ImpactPoint;
}

Blueprints have a similar method to call. Search for “Get Hit Result Under Cursor by Channel”. (Don’t forget to turn off context sensitive search). The parameters are pretty much the same as the code sample, though the screen coordinates are already implied to be the mouse coordinates so they aren’t a parameter.

The last thing to note is the trace channel. This is sort of like the category your collision ray will fall under and determines what kinds of objects it will intersect with. You can probably get away with using the default visibility channel, but you can also create your own custom channel to filter out unwanted hit types (such as intersecting only with your game objects, not terrain or trees, etc).

Get Hit Result Under Cursor by Channel did the trick for me. Is there a way to get multiple objects and not just one? like stuff behind other stuff?

Sort of like a rail gun?

Yeah, you just create a new line at the last hit location and continue along on the ray direction. That’s the easy way.

The hard way is to probably write your own variation of the intersection ray within the engine.