Laser Range Finder using an infinite Line Trace (Ray Tracing)

**
TLDR: How do I raycast from my crosshairs infinitly and get the distance to whatever it hits no matter what. **

Hi there, you can see my project in my siggy. This requires ray-tracing for many things. One of those is the option to upgrade to “prototype” binoculars and periscope with a laser range finder you buy from the tech tree in the 1970’s,

So I understand how ray-tracing works and was able to say get the distance from myself (first person character) to any actor, but how could I make it so if it hits ANYTHING (like the ocean mesh; im using the community ocean plugin) it will give me the distance to it. I used GetDistanceTo() to acheive a hit on an actor like say a cube in the level which would be fine for basic gameplay but I’d like to be able to shoot a ray out at the ocean mesh for example and get the distance to exactly where the crosshair is pointed.

I thought about faking it and doing some trial and error but “OutHitTime” but that was giving me inconsistant results.

Here is how I acheived it in Unity (bad word I know sorry! lol)



// Called after the Frame is drawn, after which Update() is called src: https://dl.dropboxusercontent.com/u/7761356/UnityAnswers/Web/HowUnityWorks/WebPlayer.html?level=FixedUpdate
void FixedUpdate()
    {
        rayDirection = transform.TransformDirection(Vector3.forward);  //this is the direction the ray is travelling (straight forward)
        if (Physics.Raycast(player.transform.position, rayDirection, out hit)) //if raycast from gameobject 'player''s position from rayDirection hits something...
        {
            disText.text = "Distance: " + hit.distance + " m";             //then Change canvas UI.Text 'text' component to read out rays distance
        }
        else
        {
            disText.text = "No Reading";  //else, if pointed at the sky change text no reading
        }
}


So I’m raycasting from the players position in the forward vector aka wherever my player is looking, and unitys raycast happens to allow us to get a “distance” reading. No such deal in Unreal (that I’ve read about or found so far).

Theres not alot of documentation on this stuff so I’d like to put a youtube video out and have this on the forums, cause I’ve searched. It seems like no matter what it wont read the ocean mesh as an actor even though its a static mesh. And the skybox fort exampolle

The only thing I’ve been able to get close is to do the get distance to on the output side, and on the input side Start the trace from get world location of first person camera and end location is GetcontrolRotation::GetForwardVector X 500,000 + GetWorldRotation but it gives me the same 518 reading whether I point at the skydome or the ocean, I beleive cause their origins are that far away.

Does your character always face your cursor? I’m only asking so that I know if the mouses’s direction and the player’s forward vector will be different. If I’m understanding what you want, you need to trace from the player for an infinite length and then get the distance between whatever was hit and the player’s current location, correct? Also, I’m not certain if you want a Blueprint or a C++ solution.

Let me know if this is what you need:

There’s a little more work to trace from the cursor’s position. I think you need to use Convert Mouse Location To World Space, it will give you world location and world direction. If you have a camera boom then I think you need to get the target arm length for it, multiply that by world direction, then add it to world location.

Will try that out thanks. The character does always face the cursor yes. This is for binoculars so I add a widget to the viewport (An image of a binocular mask, and a text box for range readout) Wil get back to you.

Never got around to testing that, will do so tonight.

I tried your idea out, it returns the same value no matter where I look unless I walk then it changes a little, so it seems to be comparing the capsule component/pawn/character to the world origin or something. I am using the First Person Shooter template with the community ocean plugin ontop as a placeholder for now.

I tried to fit all this in one image, on the left the create binocular widget while B is held followed by your method attempt, underneath the method I got working just its quite innacurate (I never thought about using vector length though will try that out this one is really ******* me off, and thats rare, in Unity it just works :frowning: I should note I’ve only been using the Unreal API constantly for about a month, just before everybody went free. This isnt a game blocker by any stretch of the word, I can still use the get distance to actor to get the distance to the hit actor and if your not aiming at anything return null, but it would be a nice addition to hte tech tree in the game to be able to upgrade your binoculars and periscope to get the range to anywhere, anything, anytime, the problem is I’m building a super realistic simulator and some nut will notice that its off by 1-10 m because they’ll be able to do the math in their head.

Heres the pic of tonights couple hours of work (all prototyping aside, just what I got “half working” and your method attempt (which i did last). I’ve been SCOURING answerhub for this I cant be the only one who needs a raycast range finder. With my half working method it will hit and leave a hit marker anywhere on the surface of anything, so thats progress I guess, before It would never hit.


At this this thread comes up on google very very high for “laser range finder” and “rangefinder” and “lincetrace infinity” and stuff. So if a solution is figured out that will help the ocmmunity I can feel good in that.

I went and got a function that I use for teleporting the character and modified it for you, I tested it and it works as it should. If I trace to the ground right next to my character I get a small number like 50, if the trace distance is large I get an appropriately large number. You’re gonna have to increase the multiplier in that vector * float node cuz right now the max trace distance isn’t very far.

EDIT: I just realize I have the two vector nodes for trace start and end positioned poorly, make sure you follow the paths.

Well I got it to work… as long as I use a plane for the ground, it wont work with the community ocean plugin though for some reason. The ocean is based off a 8191x8191x0 plane and the skybox is just a regular skydome, both the skybox and ocean give me readings of “382” no matter where I aim. Anything else works fine though.

I guess its just a problem with not being able to hit the ocean for some reason, when I open the SM_Ocean plane its 0 units in the Z so maybe its not thick enough to hit, even though I can “see” it maybeits too thin for a ray trace.


You can see some of the other ways I tried to get the length.

Thanks regardless!

Edit now it wont even work at all again no debug lines or anything. Weird. Raytracing in Unreal is the first thing I’ve hated since switching from Unity.

Not all objects respond to line trace, I’m guessing that the skybox ignores it. If you open up it’s BP and select the sky sphere mesh you will have these settings:


I think it’s a lack of collision that causes it to ignore the trace. I’d guess the same is true for your ocean mesh. I’m entirely certain what you need to modify, but it probably needs a collision preset and maybe the simulation generates hits and generate overlap. Not sure though.

The ocean is set to block all dynamic, and the skysphere to blockall. I bet that is the trick! I will play with it when I get back to messing around with it, I just learned something very useful about UE today, thank you so very much I wish we had a karma system here.

I just realized that if you expand the “collision presets” you can see what it responds to for each setting. BlockAll would be what you want as I understand it, you need to block the trace. I tried to get it to work but whenever I used trace distance long enough to reach the sky sphere it would malfunction and wouldn’t spawn at the correct location or with the correct end location. I couldn’t get it to work. Sorry, someone who knows more than I will have to figure it out.
Capture7.PNG

I don’t know if it would work where the trace does not, but you could fire an invisible projectile at a high velocity and see what it hits.

ITs not a major thing, I can still get the correct distance to any part of a ship, thats the core functinoality, I just wanted an extra upgrade on the tech tree. Thanks for your help! This page should help whenpeople google raytrace infinity unreal on google I hope.