Project position onto direction

I’m trying to make a simple function that will take an actor’s position and get the distance from that to the camera’s forward direction. I hope that made sense.

I’m doing this so that I can have my interaction system only interact with the item that the player is looking at, or at least the closest item that they are looking at.

Here is the function I am using, with both variations

323018-unknown.png

Before today, I didn’t know you could do this so an explanation would be nice, but any help would be appreciated.

This sounds like something I submitted for a game jam - I’ll have to find that and get back to you. First - What’s the question exactly? Do you want to know how the blueprint you posted works or how to get that blueprint to do what you are describing?

If possible both would be very useful. I like to know how something works as well as how to get it to work, but if that’s not possible then just how to get it to work please

Hi, sorry to bother you again, but I’d really appreciate the help

Hey, no bother.

Another question, though - Is there a reason you want to get the distance from the actor to the player camera and not from the player camera to the actor?

My thought is that if you start placing lots of this actor in the world, having each actor tracing to the player would be more expensive than just having the player character make those traces.

Then, you trace from the player every time you want to interact, or at all times to determine what can be interacted with.

Any more information about what you’re trying to accomplish would be helpful for me to help you better!

This is how I would approach calculating distance between the camera and some object:

I’ll try to explain briefly how this works without getting too much into vector maths. I’m not the right person to be teaching that subject…

Please excuse me if I’m being too basic here! I’m going to assume no knowledge of the subject.

Vectors are points in space represented by 3 values: x, y and z. They can also be used to point in directions. Unreal is x forward. Notice the tooltip on Get Actor Forward Vector states it has a length of 1.

Getting the forward vector is the same as getting the direction the rotation is pointing in. Try to print just the forward vector - if your character is facing straight along the x axis, you’ll get 1 for x, 0 for y and 0 for z.

So, the following is equally valid:

Remember how the forward vector has a length of 1? That means it’s basically a point in space that points in some direction - in this case, where our camera is pointing. Multiplying this vector by some number value will move this point in that direction by a distance equal to that number.

You with me?

That’s an absolute position, though, and if we were to plot it it would start at the world origin. We still need to get that point to be relevant for our purposes, which in this case is getting a line from the camera to some point in the distance of where the camera is looking.

To do that, we have to add the vector to our camera’s current location.

And now with this information we can feed the line trace what it wants - a start and end vector. The camera’s location being the start and the end being our vector created by adding our camera’s forward x direction multiplied by some length and added to the initial location.

Phew!

Getting the distance is less of a calculation on our part and more of breaking the hit result and getting the distance which is already calculated for us. Thanks Unreal Devs!

If this has been helpful, please mark the answer as accepted or if you need more clarification just let me know!

Happy developing,

Reishi

Maybe this will help with explaining what I want to get from this function:

The blue circle is the player, the black lines are the vectors coming from it, with the vertical one representing the forward vector, and the black circle is the actor I want to check.

I want to get the length of the grey vector shown

oh ok, I completely misunderstood the question. But we can build on that to come to the correct solution.

Thanks for the illustration! I think I know what you want to do now and its possibly quite simple. I hope this suggestion works for you:

Using the node “Get Point Distance to Line” we only need the actor’s location, a starting point for the line, and the vector it points off to. This function will calculate the distance between our location and the closest point on that infinite line - which is exactly what you are describing.

Hope this was more helpful!

That is insanely useful. Thank you so much.

Glad I could help :slight_smile: please mark the answer as accepted so we both build reputation on Answerhub for asking good questions and providing good answers!

Happy developing,

Reishi