Changing direction

If you were to compile the Blueprint, save, and play the game now, what would you expect to see? The target Cylinder would move according to our speed and direction as soon as the game began. However, since we don't have any instructions that cause the target to stop moving, it would proceed in the same direction for as long as the game runs, even moving through objects and out of the Level we created! To address this, we need logic that will change the target's direction periodically. This will result in a target that moves back and forth between two points regularly, much like a shooting gallery target.

To do this, we have to set up two nodes that will set the direction variable we created on two different values. Drag the direction variable into empty grid space and choose the SET option. This results in a node with x, y, and z axis fields. We can use them to change the value of the direction variable to be different from the initial default value that we gave it. We want two of these nodes, so once again, drag the direction variable into empty space, and then change the axis values of the two nodes to 10.0 and -10.0 respectively.

Now, we need a way to switch between these two nodes so that the direction repeatedly shifts. The FlipFlop node was created for scenarios where we know we want to alternate between two sets of Actions that execute exactly once before switching each time. This fits our use case here, so right-click on empty grid space and search for FlipFlop. Select and place the node. Then, connect the A execution pin to one of the direction set node input pins, and the B execution pin to the other.

Finally, we need to ensure that there is some kind of delay between executing the direction shifts, otherwise, the direction will change for every frame and the object will go nowhere. To do so, drag the input execution pin of the FlipFlop node into empty space and search for the Delay node. This node allows us to set a delay duration in seconds that postpones the following execution commands by that length of time. Place this node before the FlipFlop node and give it a duration of 6 seconds. Placing this Delay node between our Set Actor Transform node and our FlipFlop node ensures that the direction switch enabled by FlipFlop only occurs every 6 seconds. The final product should look like what is shown in the following screenshot. Once you are done, remember to compile and save the Blueprint:

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

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