Using a Single Line Trace (Raycast) by Channel

Choose your OS:

LineTraceByChannel 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 SingleLineTraceByChannel.

Steps

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

  2. In the FirstPersonBP/Blueprints folder, open the FirstPersonCharacter Blueprint.

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

    GHT2B_1.png

    This will cause the trace to run every frame.

  4. Drag off the execute pin, then search for the LineTraceByChannel node.

    GHT2B_2.png

  5. While holding down the Ctrl key, drag in the FirstPersonCamera component.

    GHT2B_3.png

    This is where we will start our trace from, the camera.

  6. Drag off the FirstPersonCamera node, and add a Get World Location node, then connect it to the Start of the trace.

  7. Drag off the FirstPersonCamera node again and add a Get World Rotation node.

    GHT2B_4.png

    Here, we are starting the trace from the location of the FirstPersonCamera, then we are getting the rotation of the FirstPersonCamera.

  8. Drag off the Get World Rotation node and add a Get Forward Vector, then drag off that and add a Vector * Float node set to 1500.

    GHT2B_5.png

    We 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 Get World Location node and add a Vector + Vector node, connecting (as shown below) to the End of the trace node.

    GHT2B_6.png

    Here, we are taking the location of the FirstPersonCamera and extending out from it, 1500 units based on its rotation and forward vector.

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

    GHT2B_7.png

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

  11. Drag off the execution out pin of the trace and add a Print String node.

    GHT2B_8.png

  12. Drag off the Out Hit pin and add search for Break Hit then add a Break Hit Result node.

    GHT2B_9.png

  13. Drag off the Hit Actor pin (off of the Break Hit Result), add a To String (Object) node, and connect it to the Print String node.

    Click image for a full view.

    This will allow us to debug print the object we hit with our trace.

  14. Click the Compile button, then play in the Editor and look at the cubes in the level.

    GHT2B_11.png

    Here, we have ejected from the First Person perspective so that you can see the view angle of the trace.

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

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 individual Actor to ignore).

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