Hello and welcome to a new year! This time I decided to bring a quick tip about using traces in Unreal Engine.
As a small side-project I have been working on a 3D platformer that uses some of the mechanics from Classic Tomb Raider and Prince of Persia Sands of Time: running on walls, hanging from ledges, jumping onto small objects, etc. For this, I am using a lot of tracing in Unreal Engine using Blueprints.
Tracing can be used for a lot of things. For example, you can use it to check if there’s a direct “line of sight” between a character’s eyes and an object, or to check if there’s objects between the camera and the player. In this particular project, I have been using it a lot to see if there are walls of specific types around the player, so I can tell the player character what to do in different situations. The image below shows some of the traces I am using.
As I said, I am using traces to check the walls around the player. For example, the green capsule coming from the player is a trace used to check if there’s a wall in front of the player. The trace can tell me the distance to the wall, and also check if the wall is the “correct” type of wall. I use this one for vertical wall-running, for example. Likewise, the purple capsule going from side to side of the player character is used for running along a wall.
Another thing is you can combine different traces, or rather, run various tracing operations at the same time. For example, that green trace I mentioned above is used to detect the wall in front of you (and when it’s detected a wall there’s a red portion to the trace), but then there’s another trace, the blue vertical capsule from the image (detection shows a yellow capsule), that I use to detect ledges so the character can hang from them.
There is a third trace above the character’s head, facing forward, used to detect if there is a wall above the ledge or not. This is something I am using to detect scenarios like these (when the ledge is stuck to the wall, where there’s a tiny space where you can slide across the wall, or where there’s actual floor above the ledge):
As you can see, I am placing the traces in different spots around the player. To do this, I use the “Start” and “End” parameters in my trace, like this:
And for the particular trace above the head, what I have done is set the start position 100 units above the character’s center, and then the end position is the forward vector multiplied by 150, so the trace is 150 units in length. Since Blueprints allows you to pull out directional vectors (forward, side, up), and rotations, you can start your trace virtually anywhere around the character, in any direction. This is a very powerful tool if you want to use it.
Another thing you can see in my tracing operation is that it’s using a Trace Channel. You can create Trace Channels in your project settings, and then use those channels to limit what objects are detectable by the traces. This way, you don’t need to worry about detecting the wrong objects.
There is something to consider, though. Traces can take a decent chunk of resources, so you should only use them when you need them, and only perform the traces you need. For example, the trace above the head is only triggered when the character is hanging from a ledge, not all the time.