Using a Single Line Trace (Raycast) by Channel

This how-to guide covers using a Single Line Trace by Channel Blueprint node to return the first Actor it hits that responds on the Visibility channel, and prints its name.

On this page

Line Trace By Channel will perform a collision trace along a given line and return the first Object that the trace hits. Below, you will find steps for setting up a Single Line Trace By Channel Blueprint.

Steps

  1. Create a new project using the Blueprint First Person template with Include Starter Content and open the project.

  2. In the FirstPerson/Blueprints folder, open the BP_FirstPersonCharacter Blueprint.

    01_OpenFPCharacterBP.png

  3. Right-click in the graph, search for and add the Event Tick node.

    Copy Node Graph

    02_AddEventTick.png

    This will cause the trace to run every frame.

  4. Drag off the execution pin, then search for and add the Line Trace By Channel node.

    Copy Node Graph

    03_AddLineTrace.png

  5. While holding down the Ctrl key, drag in the First Person Camera component.

    Copy Node Graph

    04_DragFPCamera.png

    The camera is where you will start our trace from.

  6. Drag off the First Person Camera pin, search for and add the Get World Location node. Connect Return Value pin of the Get World Location node to the Start pin of the Line Trace By Channel node. Your script should look as following.

    Copy Node Graph

    05_AddWorldLocation.png

  7. Drag off the First Person Camera pin again, search for and add the Get World Rotation node.

    Copy Node Graph

    06_AddWorldRotation.png

    In this case, you are starting the trace from the location of the First Person Camera, then you are getting the rotation of the First Person Camera.

  8. Drag off the Return Value pin of the Get World Rotation node, search for and add the Get Forward Vector. Drag off the Return Value pin of the Get Forward Vector, search for and add a Multiply operator node. Set it to 1500, as shown below.

    Copy Node Graph

    07_AddFwdVector.png

    You are getting the rotation and the forward vector, extending outward from it by 1500 (this value is the length of the trace).

  9. Drag off the Return Value pin of the Get World Location node, search for and add the Add operator node. Connect B Vector pin of the Add node to the Return Value pin of the Multiply node, then connect Return Value pin of the Add node to the End pin of the Line Trace By Channel node. Your script should look as following.

    Copy Node Graph

    08_BPScript.png

    Here, you are taking the location of the First Person Camera and extending out from it, 1500 units based on its rotation and forward vector.

  10. On the Line Trace By Channel node, set the Draw Debug Type to For One Frame.

    09_ChangeDrawDebugType.png

    This will allow you to see a debug line while playing in-game to see our line trace.

  11. Drag off the execution out pin of the Line Trace By Channel node, search for and add the Print String node.

    Copy Node Graph

    10_AddPrintString.png

  12. Drag off the Out Hit pin of the Line Trace By Channel node, search for and add the Break Hit Result node.

    Copy Node Graph

    11_AddBreakHitResult.png

  13. Connect the Hit Actor pin of the Break Hit Result node to the In String pin of the Print String node. Your script should look as following.

    Copy Node Graph

    12_BPScriptFinal.png

    This will allow you to debug print the object you hit with your trace.

  14. Click Compile and Save buttons.

    13_SaveCompileButton.png

End Result

The example above returns all Objects that are set to respond to the provided Trace Channel, however, there may be instances when you want to only return certain objects. In the example above, you can use the Actors to Ignore pin, taking an Array of Actors that should be ignored by the trace (this means that you have to specify each Actor to ignore).

You should see that when the trace hits a cubes, it prints the cubes to the screen.

![](14_FinalResult.png)

You can also perform a Line Trace By Object where only the specified Object Types are returned. This will allow you to target a specific set of Objects (only to be included in the trace).

Help shape the future of Unreal Engine documentation! Tell us how we're doing so we can serve you better.
Take our survey
Cancel