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