Getting our barrels rolling

With our level blocked out we can begin to place our Gameplay-centric actors into the level and add the appropriate functionality to them. In this case, we will be creating the barrels. We are going to need to create a barrel object that rolls with physics, destroys the player when the two collide and destroys itself when it reaches the bottom of the level. We are also going to have to create an object that handles spawning these barrels at certain intervals.

The first thing we need to do is create a new blueprint that inherits from Actor called BH_Barrel. It is going to be a physics-driven actor that will be one of the key elements in the game world, so create this new blueprint now. BH_Barrel will start with the same default components we are used to seeing when working with a new actor. We need to add a visual component to our BH_Barrel. Add a Cylinder shape via the Add Component button now. The Cylinder will be used as our Barrel Mesh. Let's make an attempt to have our grey scale cylinder look a bit more like a barrel by applying the M_Wood_Floor_Walnut_Worn material to the mesh. You can do this by selecting the Cylinder component and modifying Element 0 under the materials section in the Details Panel. Also, rotate the Cylinder component around the y-axis by 90 degrees so lays on its side. You should be presented with something similar to this:

Applying physics to objects

Adding physics dynamics to an object in UE 4 is actually quite simple given a basic 3D shape. In the case of our Barrel, we already have the perfect shape, a cylinder! What we are going to do is enable physics simulations on our Cylinder component. We can do this by selecting the component and navigating to the Physics section of the Details panel. At the top of the section is a checkbox titled Simulate Physics, tick this now. Bam! You have applied physics dynamics to this object! The UE physics layer will now handle all interactions between this object and other physical objects in a 3D scene given the right collision settings.

There are a few more steps we need to take before we are ready to spawn our BH_Barrels in the game world. We also need to constrain our barrel so that is does not move in an undesirable way. Because we are creating a side scroller, we only want freedom of movement on a certain plane. In our case, it is the YZ plane. The axis along which the character can move (x-axis and z-axis) are also the only axis we wish our physics bodies to have freedom of movement along. To constrain our body in this way, expand the Constraints field under the Physics section of the Details panel and select YZPlane from the drop-down list titled mode. The Physics section of the Cylinder's Details panel should appear as follows:

Barrel spawners and Blueprint timers

Now, we need to create the object that will spawn the BH_Barrels, so they can roll down the ramps and act as hazards to the player trying to reach the top of the level. This will be a simple object that only consists of the default components. However, we will be utilizing a new event in the graph of this blueprint. We will be using Event Tick to set up a timer so that this object spawns a new barrel whenever a certain threshold of time is reached.

Creating Blueprint variables

Create a new Blueprint that inherits from Actor now, and call it BH_Barrel_Spawner. We are not going to add any new components to this object, so you can immediately navigate to the Event Graph. To set up our timer, we are going to create two new variables of type float. To create these variables, click on the small white plus next to Variables in the My Blueprint panel. By default, the Blueprint will create new variables of type boolean. Click on the white plus twice now and call the first variable barrelTimer and the second timeSinceLastBarrel. We need to change the type of these variables to be float. We can do this by selecting the variables within the My Blueprint panel under the Variables section and addressing the Details panel. The Variable Type property can be changed via the provided dropdown. Change the type of both of these variables to Float now, which is denoted by a green bar symbol in the dropdown.

From this details panel, we are also able to modify the various properties of the variable. You will note that we are able to set the Default Value of this variable. The value we provide here will be the value that is used upon object creation. This means we can specify a time in seconds that we wish to use as the rate at which barrels will spawn. For now, set the default value to 3.0. Create another float variable now and name it timeSinceLastBarrel and set the Default Value to 0.

Event tick

Now, we are going to use those two variables in combination with Event Tick to create our timer. The Event Tick node is provided by default in the event graph; however, it should currently be translucent as there is no functionality being extended from the node. The Event tick node outputs a float variable called Delta Seconds. This variable represents the time in seconds since the last frame, meaning that every time this event is hit it provides you with the change in time in seconds since the last time the event was hit. We can accumulate the value of this variable over time to gauge how much time in seconds has passed in total. What we are going to do is accumulate delta seconds into the variable timeSinceLastBarrel; then, when the value saved within is larger than our barrelTimer value, we will spawn a BH_Barrel.

The first thing you need to do is add Delta Seconds to timeSinceLastBarrel and then save the result back into timeSinceLastBarrel. You can do this by creating a get reference to timeSinceLastBarrel, then dragging a line off of the output pin from the reference, and then search for float + float. This will give you a + node with two green inputs and one green output. By default, one of the input pins is populated by timeSinceLastBarrel. Plug Delta Seconds into the other input pin. From the output pin, click on and drag off another line, and this time, search for Set Time Since Last Barrel and parse in the result of the + node.

Now, we need to check if our accumulated value is larger than our timer value. To do this, drag from the output pin of the Set node we just created and summon the float > float node. This node features two float input values, the top input will be compared against the bottom, that is, is top > bottom. This node will output a Boolean variable that will be the result of this condition. What we need to do is plug this value into a branch node. Before we go into branch nodes, lets quickly check your Blueprint is coming along in the right direction. At the moment, you should see something similar to this:

Branch nodes

Branch nodes act in a very similar way to if statements in C++. Drag off from the output pin of the > node and search for Branch. This will present you with a new type of node. This is a logical node and can affect the critical execution path. As you can see, it takes in a Boolean input as well as the execution path but outputs two execution path options. One for when the input pin has a value of true, and another when the input value is false. From the true execution path option, drag out a line and search for Spawn Actor From Class. This node will spawn our BH_Barrel. The node itself takes in a few inputs they represent the following:

  • Class: The type of object you wish to spawn. Set this to BH_Barrel now.
  • Spawn Transform: The transform we wish the object to have upon spawning. Summon a node called GetActorTransform and plug the orange output pin into this input.
  • Collision Handling Override: This is an enum that represents how the object will handle collision if it is spawned inside another object. Leave this as default.
  • Instigator: A reference to a pawn that is responsible for damage caused by the spawned actor. Leave this one blank.

Finally, drag another execution line from this node and then set our timeSinceLastBarrel variable back to zero via a set node. Your second half of the graph layout should look similar to this:

Now, let's drag BH_Barrel_Spawner into our level and see if they work! Return to the Editor window and place one spawner about 450 units above where each ramp meets a wall. With all of the spawners in place, press the Play button in the Toolbar panel then immediately press the Eject option that appears. This will let you fly around your level mid-simulation without being constrained to the Default Player Pawn. You should be able to see a legion of barrels being spawned every 3.0 seconds! You will also notice a very immediate problem. There is nothing that can get rid of our barrels, so they just keep building up!

Trigger volumes and destroying Actors

What we need to do is place some trigger volumes in the scene that will destroy any barrel that enters its bounds. A trigger volume is an area in 3D space that will fire an event if any actor overlaps the 3D volume. We will be creating volumes that are cuboid in shape. From the Place tab of the Modes panel, navigate to the Basic section. From here, you will see an object called Box Trigger, click on and drag a box trigger into the scene now. Call this new trigger BH_BarrelKiller.

Then, click on the Blueprint/Add script button. This will create a new Blueprint titled BH_BarrelKiller_Blueprint. Again, we are not going to be adding any components to this new blueprint. We are only going to be extending functionality from the Event ActorBeginOverlap node.

What we need to do is check if the actor that has just overlapped our new trigger volume is of type BH_Barrel. If it has, we need to destroy the actor! Doing this is easy. Drag a line from the Other Actor output pin featured on the ActorBeginOverlap node and search for a function titled GetClass. This node has an output pin that is the class type of the object that is parsed into the node. We can use this class type output pin to determine if the object is of type BH_Barrel.

Drag a line from this class output pin and search for Equal (Class) or type = for short. This will spawn a class comparison node that will check if the top class input is equal to the bottom class input. By default, the top input pin will be populated with the class pin we just dragged from; the bottom pin we will not connect. Simply select BH_Barrel from the provided drop-down list. The comparison node will have a Boolean output pin that will represent the result of the comparison; if the comparison was resolved to be true, we want to destroy the overlapping actor.

We will do what we did in our timer and create a branch node from this Boolean output. From the true execution path, search for a node called DestroyActor. This node takes in an actor to destroy. For this input pin, we will drag the Other Actor reference from the Event ActorBeginOverlap node. Instead of dragging this pin directly, we are going to keep our graph tidy by utilizing something called a reroute node. It is simply something we can use to steer the paths of our graph lines. After dragging from the output pin of the ActorBeginOverlap node type reroute and select the Add Reroute node… option. Use these reroute nodes to create a clean path from the Event node to the final Destroy Actor function node. I create a path that looked like this:

You will notice that you can have multiple lines from an output pin. This is because this reference can be accessed by multiple things along the execution path.

Tip

If you want to remove all lines from a pin in one go simply hold Alt then click on the pin you would like to remove lines from. Similarly, if you want to shift multiple lines to a different reference, hold Ctrl and then click and drag from the pin.