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.
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.
Add a Comment