Using Raycasts (Tracing) in Blueprints

On this page, you will learn the difference between Single and Multi Raycasts as well as how to set up each which could be used to retrieve collision information.

Single Line Trace

LineTraceByChannel

The LineTraceByChannel will perform a collision trace along a given line and return the first Object (that responds to the Trace Channel) the trace hits.

Below you will find steps for setting up a Single Line Trace by Channel.

  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 and 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 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 then extending outward from it by 1500 (this value is the length of the trace).

  9. Drag off the Get World Location and add a Vector + Vector node and connect 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. Off the Hit Actor pin off 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 print to the screen 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.

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 which takes in an Array of Actors which should be ignored by the trace, but this would mean 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.

LineTraceForObjects

The LineTraceForObjects will perform a collision trace along a given line and return the first Object (that responds to the ObjectType) the trace hits.

To set up a LineTraceForObjects trace, do the following:

  1. Follow the steps used for the LineTraceByChannel example to set up a trace.

  2. Replace the LineTraceByChannel node with the LineTraceForObjects node.

  3. Drag off the Object Types pin and add the Make Array node.

    GHT2B_12.png

  4. On the Make Array node, specify the ObjectType you want to trace for via the drop-down menu.

    GHT2B_13.png

    Here we are tracing for WorldDyanmic Objects. You can add more by clicking the Add Pin button.

  5. You can set the rest of the trace up the same way a LineTraceByChannel is setup.

    Click image for a full view.

  6. We have added a single WorldDynamic Object into our level.

    GHT2B_16.png

    Only the added Actor is now returning as a hit, the cubes since they are Physics Actors do not return a hit.

Multi Line Trace

MultiLineTraceByChannel

The MultiLineTraceByChannel will perform a collision trace along a given line and return all hits encountered up to and including the first blocking hit, returning only objects that respond to the specified Trace Channel.

Below you will find steps for setting up a MultiLineTraceByChannel.

  1. Follow the steps used for the LineTraceByChannel example to set up a trace.

  2. Replace the LineTraceByChannel node with the MultiLineTraceByChannel node.

  3. Drag off the Out Hits pin and add a ForEachLoop node.

    GHT2B_18.png

    Since multiple Actors are hit, for each of them we want to do something (in this example, print the Actors to the screen).

  4. Drag off the Array Element and add a Break Hit Result, then off the Hit Actor, add a To String (Object) and connect to a Print String.

    Click image for a full view.

    This will now print to the string each of the Actors hit by the array.

  5. Here we have a Glass Window in front of a Physics Actor.

    GHT2B_17.png

    The Glass Window is a Destructible Mesh and we have set its Trace Response to Visibility in its collision settings is set to Overlap, while the Physics Actor (Cube) has its Visibility setting to Block.

    You could use this for situations where you may want to shoot through an object (destroying it) and also hitting a player or enemy.

MultiLineTraceForObjects

The MultiLineTraceForObjects will perform a collision trace along a given line and return all hits encountered, returning only objects that respond to the specified Object Type.

Below you will find steps for setting up a MultiLineTraceForObjects.

  1. Follow the steps used for the LineTraceByChannel example to set up a trace.

  2. Replace the LineTraceByChannel node with the MultiLineTraceForObjects node.

  3. Drag off the Object Types pin and add a Make Array node, then use the drop-down menus to add Objects to the Array.

    GHT2B_21.png

    Here we assign WorldDynamic and Physics Body as our Object Types. You can use the Add pin button to add more Object Types to the Array.

  4. Off the Out Hits pin of the trace node, add a ForEachLoop node.

    GHT2B_22.png

    This will allow us to do something for each of the Actors hit by the trace.

  5. Drag off the Array Element and add a Break Hit Result, then off the Hit Actor, add a To String (Object) and connect to a Print String.

    Click image for a full view.

    This will now print to the screen each of the Actors hit by the array.

  6. Here we have a hanging ceiling light (World Dynamic Object Type) in front of a Physics Actor (Physics Body Object Type).

    GHT2B_20.png

    The Multi Line Trace by Object, unlike the Multi Line Trace by Channel, does not stop at the first object it hits which is why the trace continues through the light to the cube.

Shape Traces

GHT2B_24.png

When a Line Trace is not enough, you may get the results you are after by using a Shape Trace instead. For example, maybe you are creating a "vision cone" for an enemy and you want to detect players that enter it. A Line Trace may not be enough as players may be able to avoid detection by ducking under the Line Trace.

In this situation, you could possibly use a Box Trace, Capsule Trace, or Sphere Trace instead. Each Shape Trace functions like a Line Trace where you are sweeping and checking for collision from a start point to an end point, however the Shape Traces have an added layer of checking as you are using a shape as a volume of sorts in your Raycast.

You can use a Shape Trace as a Single Trace or a Multi Trace and each are set up in the same manner as a Line Trace however you will need to provide additional details pertaining to the size or orientation of the shape you use.