How do I find the distance to a target along the players aim, if the player is not looking directly at the target?

How do I find the distance to a target along the players aim, if the player is not looking directly at the target?

Ive set up a targeting function that currently gets the closest target within an FOverlapResult.
I need the target closest to the players aim, not the player themselves.

I think(mathematically) this is a matter of finding the longer cathetus of a right angle, where the hypotenuse is the actual distance to the target.

Im not familiar with what functions, if they exist, would do this.

Hi,

There is a function is UE that calculate the shortest distance between a point and a line.

Use it between your target position and your aim vector.
be aware that the line will be considered as infinite it s not a segment.

If I remember properly it s in the Math part.
I m not at home so I can t give you the name but use the search and I bet you will find it…

it’s FMath::PointDistToLine

Thank you! That works nicely.

Bit of an update:
I don’t know if its intended, but FMath::PointDistToLine does not actually get a point along the line.
It seems to get a point on the opposite side of the targetpoint from the line.

Fortunately, I was still able to make some custom code based off this to meet my needs.