Example of vectors and trace nodes

Let's do an example to see the use of vectors and trace nodes. We will modify the Player Character to use a line trace to find and toggle the light of another Blueprint:

  1. Create a new project using the First Person template with starter content.
  2. Open the FirstPersonCharacter Blueprint, which is located in the /FirstPersonBP/Blueprints folder.
  1. In the My Blueprint panel, create a new Macro and name it Trace Locations. In the Details panel, add two output parameters of the Vector type. Name the parameters  Start Location and End Location, as shown in the next screenshot. We do not need input parameters:

  1. Double-click the name of the Macro to edit it. Add the Actions of the following screenshot. This Macro calculates the Start and End locations that will be used by Line Trace. Since this is a first-person game, we are using the camera as Start Location, and End Location is 300 cm ahead of Camera:

  1. Click on the Event Graph tab and add an input Event for the Enter key. Add a LineTraceByChannel node and add the Trace Locations Macro to the Event Graph. Connect the outputs of the Macro to the Start and End inputs of Line Trace, as shown in the next screenshot:

  1. Connect the Actions of the following screenshot to the output of the LineTraceByChannel node. These Actions test whether Hit Actor is of the Blueprint_WallSconce type. If it is, then the light of Blueprint_WallSconce is toggled. Compile the Blueprint to apply the changes.

  1. Add an instance of Blueprint_WallSconce (which is in the /StarterContent/Blueprints/ folder) to the Level, and then play the Level. Move your character close to the instance of Blueprint_WallSconce, look at it, and press the Enter key to toggle the light. The next screenshot shows the Player Character and Blueprint_WallSconce:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset