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 to two different values. Drag the direction variable into empty grid space and choose the Set option. This will result 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 drag the direction variable again into empty space, and then change the Y 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 will postpone 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 will ensure that the direction switch enabled by FlipFlop will occur only 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.
Testing moving targets
Now that we have our Blueprint updated, we can test to ensure that the CylinderTarget
object moves as expected. First, we will have to place the CylinderTarget
object in a position that will allow it to move along the Y axis without bumping into other objects. The coordinates I used were 410
on the X axis, 680
on the Y axis, and 180
on the Z axis.
Note that these values will only work relative to the default layout of the First Person
template map. If you have made adjustments to your own level, then you can adjust either the speed or the placement of the target in your level, and test until you find a good patrol spot. Click on Play. If the Blueprint is functioning correctly, you will see the cylinder move back and forth between two points at a steady rate.
One of the advantages of using Blueprints is that they create a template of functionality that can be used across multiple objects in a scene. Find CylinderTarget_Blueprint
in the Blueprints
folder and drag it directly onto 3D Viewport. You will see another cylinder created, which inherits all of the functionality of our original cylinder target. In this way, we can rapidly set up multiple moving targets using the single set of Blueprint logic we created.