Gamedev Toolkit: Line Trace

Greetings game devs! Today we’ll be talking about Line Traces.

Why line traces? Because they’re an incredibly useful tool for all manner of things but most importantly, I keep running into devs at game jams and students I mentor that don’t even know this tool exists. So I figured It’d be a good post to write up and throw at people.

Mind you I am primarily going to be talking from an Unreal Engine perspective but it works the same throughout.

Line Trace? What's that?

A good way of describing a line trace would be a laser pointer. You point it in a direction and it’ll fire off to whatever reach it’s configured for. As the trace goes out it will report back on what it hit, where it hit, what kind of thing it is, and much more information.

But just what could you use this information for? Why is a line trace an useful tool? I’ll start with how I learned about line traces being a thing, a problem that I keep seeing people having. Shooters! Specifically Third-Person-Shooters.
 I was working on Death By Oil, one of the projects here on my portfolio, and that was a TPS where players control a big stompy mech. The problem we ran into was getting the bullets to go from the barrel of our mech’s guns to where the player was pointed at. We tried all sorts of tricks involving the placement of the camera and where the bullets spawned from. Originally, rounds spawned from the barrel of the gun but as you can imagine they never hit the target at range without some serious compensation from the player. Then we tried shooting from the center of the camera, but that didn’t look nearly as awesome as watching the tracers leave the guns.
Line tracing was the answer to our problems here and it was so simple to use too! We merely fired off the trace from the center of the camera to the max range of our weapons. If it hits something it sends back the hit location, if it didn’t hit anything it sent back where the trace ended, and then the projectile is fired from the barrel of the gun. This way the player would be confident in their rounds going where they wanted while maintaining the effect of munitions leaving the barrel of their guns and made for an infinitely better user experience than our previous implementations.

How complicated is this to set up?

Not very!

On the basic end of things you can fire off a line trace from the camera like we did on DbO or you can spawn it from a component on a character. In UE4 I usually use an arrow component so I have a good idea as to where the line will go when it’s fired.  You can also have it fire off from other attachments on the character like a gun they picked up.

Of course, then you have to choose what kind of line trace this is. Are you looking for things by channel, object, component, etc. This is basically just to decide what to include or exclude from hit results. For example you can set a line trace to ignore something like glass. It’s a physical object sure, but the target you’re aiming for is behind it, so the line trace will pass through the glass, hit the target behind it and send back the information required by your code.

Line Trace is a really simple tool to use!

What else could you use line traces for?

All sorts of things actually!

Need to pick up an item a player’s looking at? A line trace with a short range is a quick and simple solution. Want your AI to hunt for the player but have their vision blocked by bushes they could otherwise run or shoot through? Line trace works there as well.

An use I was tinkering with about a week or so ago was wall jumping. I was trying to replicate the Mega Man wall jump for giggles and originally I was planning to use a secondary collider around the player character for this purpose. The problem was it worked from every direction where Mega Man’s jump only works when he is facing a wall. So I fired off a line trace from the character’s feet and boom! Problem solved.

Another use could even be for wall running. Need to check if your character is running along a wall on their left or right? Fire off a line trace from either side, whichever of the two hits the wall sends back true and then boom, play the animation for running on the left or right side of the character. Easy.

What about something more complicated? Well this simple tool is quite flexible so the limit is entirely up to your imagination and technical skill.

For example you could use a line trace to spawn another line trace that spawns another line trace, etc. To simulate a beam bouncing from one reflexive surface to another. You could have an ability that rains fire from above, but you don’t want that ability to say, spawn through the ceiling and be absolutely ineffective. A line trace can be used to check for low ceilings and be used to adjust the spawn point of this ability.

I could keep going with more examples of line tracing and how useful it can be, like for debugging, melee attacks and the like, but I believe you get the idea. It’s a great tool to have on your tool belt as a game dev for all manner of applications and one that will spare you many a head ache.
That’s it for today! I’ll catch you next week where I’ll talk about time management. It’s an important skill for everyone, especially when life throws curve balls at your plans.
See you then!

Add a Comment

Your email address will not be published. Required fields are marked *