Line Trace by Channel | Unreal Engine Blueprints

Tracing or raycasting in Unreal Engine is one of those complex blueprint topics that can initially be overwhelming. However, once you understand how it works, it will significantly enhance your game development possibilities.

I will start by explaining how a Line Trace by Channel works, as it is the most common way to trace in Unreal Engine. I plan to expand on this lesson in the future, so stay tuned.

What are Traces in Unreal Engine?

Traces, such as the Line Trace by Channel, are used to create interactions between Blueprints, typically connecting the player with different objects within the game world.

This interaction is achieved by an invisible line that usually starts from the player’s position and then interacts with anything that touches that line. The length of the line is entirely customizable.

Line Trace by Channel Third Person

How to create a Line Trace by Channel

Line Trace by Channel (First Person Camera)

For this example, we will start a line trace from the camera of the player.

This is the most common method used in first-person games, as the camera is usually placed in the head of the character.

Line Trace by Channel First Person

Next, we will need to calculate the camera’s location and the direction in which the player is looking, as we want the line to go forward, not in other directions.

To do this, just drag and drop your camera into your Event Graph and connect it to these two blueprint nodes: ‘Get World Location‘ and ‘Get Forward Vector‘.

In the next steps, you will need to calculate where the line starts, where it will end, and its length.

Line Start

To define the starting point of the line, connect the ‘Get World Location‘ blueprint node to the ‘Start‘ input pin of the Line Trace by Channel, as the line will begin where the camera is placed.

Line Length

Then, define the length of the trace line by creating a float variable with a random number, usually between 600 and 1600 units, but feel free to experiment as much as you want.

After defining the value, multiply it by the ‘Get Forward Vector‘ output.

This way, the line will extend in the direction where the player is looking, expanding its length by the defined number of units stored in the variable.

Line End

To finish, add the ‘Get World Location‘ to the result of the multiplication and connect everything to the ‘End‘ input of the Line Trace by Channel node. With that, the end of the line is defined.

As you can see, where the line starts and ends is entirely customizable depending on your character and game.

Line Trace by Channel starting from the camera position

Line Trace by Channel (Third Person Actor)

But what happens if the camera is placed far away from the character?

This is the typical scenario in all games that are not in the first-person view.

In such cases, we don’t want to start the line from the camera position but from the player’s position.

Let’s take a look at how to do it.

Line Trace by Channel Third Person

The first thing you need to do is create the three blueprint nodes we are going to connect to the Line Trace by Channel: ‘Get Actor Location,’ ‘Get Actor Rotation,’ and ‘Get Forward Vector’.

Using these three nodes, we will be able to determine our character’s position, rotation, and the direction it is facing.

Line Start

To create the starting point of the line, connect the ‘Get Actor Location‘ node to the ‘Start‘ input of the Line Trace by Channel.

However, if you want, like me, to start the line a bit higher, not at the feet of the character, you can add a ‘Break Vector‘ Node.

Right-click the ‘Start‘ input, split the struct pin, and connect everything except the Z value.

For the Z value, representing the height where the line will start, create a simple float variable, set the player’s height, and then add it to the Z value. If it gets a bit confusing, take a look at the screenshot below.

Line Length

Then, define the length of the trace line by creating a float variable with a random number, usually between 600 and 1600 units, but feel free to experiment as much as you want.

Next, connect the ‘Get Actor Rotation‘ node to the ‘Get Forward Vector‘ node.

With the trace length variable ready, multiply it by the output of the ‘Get Forward Vector‘ node.

This way, the line will extend in the direction where the player is rotated and looking, expanding its length by the defined number of units stored in the variable.

Line End

To finish, add the ‘Get Actor Location‘ to the result of the multiplication and connect everything to the ‘End‘ input of the Line Trace by Channel node. With that, the end of the line is defined.

Line Trace by Channel starting from the actor position

How can I tell if the line trace has hit something?

To find out if the line touched something, just check its color while playing the game.

If the line hits an object with collision, it changes from red to green.

Tip: You can change the line trace colors in the ‘Trace Color’ and ‘Trace Hit Color’ inputs.

The spot where the line and the object meet is marked with a small red cube.

Line Trace by Channel Third Person

Debug Options (Draw Debug Type)

To test the line trace during gameplay, utilize the ‘Draw Debug Type‘ options available in the Line Trace by Channel node:

  • None: No trace lines will be displayed on the screen.
  • For One Frame: The trace line will be shown for only one frame on the screen. This is particularly useful if you are executing the Line Trace using Event Tick, as you will have only one constant line displayed on the screen.
  • For Duration: The line will be displayed for the duration you set up in ‘Draw Time‘.
  • Persistent: The line traces stay on the screen and won’t disappear. This is not recommended, especially when working with Event Tick, as your screen will quickly become cluttered with lines.
Line Trace Draw Debug Type UE5

Out Hit and Break Hit Result

To check if the line trace hit something, we have two options:

  1. Connect the ‘Out Hit‘ pin to a ‘Break Hit Result‘ node.
  2. Or simply right-click on ‘Out Hit‘ and select ‘Split Struct Pin‘.

In both cases, you will receive a long list of values that you can access, depending on which actor the line has hit.

However, in most cases, especially if you are a beginner, you will be using the ‘Hit Actor‘ output pin. This pin will return which actor has interacted with our line.

For the sake of keeping this example easy to understand, I am casting to all the static meshes in the game. This action will Print a String on the screen when the line touches a static mesh in the level.

However, it’s advisable to avoid casting here as much as you can. Replacing the casting with an interface is the right way to go.

You can learn more about Interfaces in Unreal Engine in our lesson.

Line Trace by Channel Hit Actor Unreal Engine

Unreal Engine Roadmap