Blueprints Visual Scripting for Unreal Engine

4.7 (3 reviews total)
By Brenden Sewell
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Free Chapter
    Object Interaction with Blueprints
About this book

Blueprints Visual Scripting for Unreal Engine is a step-by-step approach to building a fully functional game, one system at a time. Starting with a basic First Person Shooter template, each chapter will extend the prototype to create an increasingly complex and robust game experience. You will progress from creating basic shooting mechanics to gradually more complex systems that will generate user interface elements and intelligent enemy behavior. Focusing on universally applicable skills, the expertise you will develop in utilizing Blueprints can translate to other types of genres.

By the time you finish the book, you will have a fully functional First Person Shooter game and the skills necessary to expand on the game to develop an entertaining, memorable experience for your players.

From making customizations to player movement to creating new AI and game mechanics from scratch, you will discover everything you need to know to get started with game development using Blueprints and Unreal Engine 4.

Publication date:
July 2015
Publisher
Packt
Pages
188
ISBN
9781785286018

 

Chapter 1. Object Interaction with Blueprints

When setting out to develop a game, one of the first steps toward exploring your idea is to build a prototype. Fortunately, Unreal Engine 4 and Blueprints make it easier than ever to quickly get the essential gameplay functionality working so that you can start testing your ideas sooner. To develop some familiarity with the Unreal editor and Blueprints, we will begin by prototyping simple gameplay mechanics using some default assets and a couple of Blueprints.

In this chapter, we will cover the following topics:

  • Creating a new project and a level

  • Placing objects in a level

  • Changing an object's material through Blueprints

  • Using the Blueprint editor and connecting Blueprints together

  • Compiling, saving, and playing our game

  • Moving objects in the world with Blueprints

 

Creating a project and the first level


Before we can begin setting up gameplay elements, we need to create a project that will contain the content of our game. To access Unreal Engine 4 and begin setting up our project, we must first open the Epic Games Launcher, which can be downloaded from the Unreal Engine 4 website (https://www.unrealengine.com/). From the Epic Games Launcher, click on the tab labeled Unreal Engine. If you are using Unreal Engine on your computer for the first time, you will see a grayed out-button labeled Not Installed. Along the left-hand side of the launcher, you will see options.

The Library tab is the location where you will be able to access the versions of the engine you have installed and the projects you have built. Now click on the Library tab, find the yellow Install button (as seen in the following screenshot), and click on it:

When the engine has finished installing, the Install buttons will change to Launch buttons, as shown in the following screenshot. Click on any of the Launch buttons to open the engine.

Setting a template for a new project

Once you click on Launch, you will be presented with the Unreal Project Browser. This will by default take you to the Projects tab, which will show you a thumbnail view of all the projects you have created, as well as any sample projects you might choose to install. For our purposes, we want to start a new project, so click on the tab labeled New Project.

From the New Project tab, you can select a template that will give you the initial assets to use for your game; or you can choose to start a blank project. You will see two subtabs under the New Project tab, labeled Blueprint and C++. Creating a project from the content within the Blueprint tab will start your project with a basic set of behavior built using Blueprints. The C++ tab is used to create projects where at least some of the core types of behavior of the game are going to be built using the C++ programming language. Since we quickly want to get a prototype first-person shooter up and running without having to build the basic controls from scratch, we should ensure that we have the tab labeled Blueprint selected. Then we choose the First Person template, as shown here:

Making sense of the project settings

The next step is to adjust the project settings to our liking. The three gray boxes below the template selector allow us to select the class of hardware we are targeting (desktop/console or mobile/tablet), the graphics scalability, and whether we want to create our project with or without starter content. Leave these settings at their default values (Desktop/Console, Maximum Quality, and With Starter Content). Below these, you will see a folder path field used to designate where you would like to store your project on your hard drive, and a name field to input the name by which your project will be known. I named the project BlueprintScripting and stored it in the default Unreal Projects folder for OS X, as shown in this screenshot:

Creating the project

Now that we have a template selected and the project settings set up the way we like, we can create the project. To do so, click on the green Create Project button. After the engine is done with initializing the assets and setting up your project, Unreal Editor will open Level Editor, where you can create and view levels, place and modify objects, and test your game as you modify it.

Pressing the Play button, as shown in the following screenshot, along the top of the toolbar, will allow you to try the default gameplay that comes built into the First Person template. This includes player movement, shooting a simple projectile, and using projectiles to apply force to primitive box objects. In play mode, the Play button will be replaced with a Pause button and a Stop button. You can press the Pause button to temporarily halt the play session, which can be useful when you want to explore the properties of an interaction or actor that you just encountered during gameplay. Pressing the Stop button will end the play session and take you back to editing mode. Go ahead and try playing the game before we continue.

 

Adding objects to our level


Now we want to start adding our own objects to the level. The central panel you see in Level Editor is known as 3D Viewport. A viewport allows you to see the 3D content of the game, and it is important that you become familiar with navigating inside this panel. The viewport can be navigated by moving the camera around using a combination of mouse buttons and its movement. Holding down the left mouse button and dragging the mouse pointer inside the viewport moves the camera view forward and backward, or left and right. Holding down the right mouse button and moving the mouse allows you to look around by rotating the camera. Finally, holding down either the middle mouse button or a combination of both the left and right mouse buttons will allow you to drag the camera up and down.

The simplest kind of object that can be dragged into the game world in Unreal Engine 4 is called an actor. An actor is a basic object with no inherent behavior other than the ability to be rotated, moved, and scaled, but it can be expanded to include more complex behavior by attaching components. Our goal will be to create a simple target actor that will change color when shot with the included gun and projectile. We can create a simple actor by going to the Modes panel. With the Place tab selected, click on Basic and then drag the object called Cylinder into the 3D Viewport. This will create a new cylinder actor and place it in our level. You should see the actor in the 3D Viewport as well as in the Scene Outliner panel, where it will be named Cylinder by default. Right-click on this object in the Scene Outliner panel, go to Edit, and then select Rename. Rename the Cylinder object to CylinderTarget, as shown here:

 

Exploring materials


Earlier, we set for ourselves the goal of changing the color of the cylinder when it is hit by a projectile. To do so, we will need to change the actor's material. A material is an asset that can be added to an actor's mesh (which defines the physical shape of the actor) to create its look. You can think of a material as paint applied on top of an actor's mesh or shape. Since an actor's material determines its color, one method of changing the color of an actor is to replace its material with a material of a different color. To do this, we will first be creating a material of our own. It will make an actor appear red.

Creating materials

We can start by creating a new folder inside the FirstPersonBP directory and calling it Materials. Navigate to the newly created folder and right-click inside empty space in the content browser, which will generate a new asset creation popup. From here, select Material to create a new material asset. You will be prompted to give the new material a name, which I have chosen to call TargetRed.

Material properties and Blueprint nodes

Double-click on TargetRed to open a new editor tab for editing the material, like this:

You are now looking at Material Editor, which shares many features and conventions with Blueprints. The center of this screen is called the grid, and this is where we will place all the objects that will define the logic of our Blueprints. The initial object you see in the center of the grid screen, labeled with the name of the material, is called a node. This node, as seen in the previous screenshot, has a series of input pins that other material nodes can attach to in order to define its properties.

To give the material a color, we will need to create a node that will give information about the color to the input labeled Base Color on this node. To do so, right-click on empty space near the node. A popup will appear, with a search box and a long list of expandable options. This shows all the available Blueprint node options that we can add to this Material Blueprint. The search box is context sensitive, so if you start typing the first few letters of a valid node name, you will see the list below shrink to include only those nodes that include those letters in the name. The node we are looking for is called VectorParameter, so we start typing this name in the search box and click on the VectorParameter result to add that node to our grid:

A vector parameter in the Material Editor allows us to define a color, which we can then attach to the Base Color input on the tall material definition node. We first need to give the node a color selection. Double-click on the black square in the middle of the node to open Color Picker. We want to give our target a bright red color when it is hit, so either drag the center point in the color wheel to the red section of the wheel, or fill in the RGB or Hex values manually. When you have selected the shade of red you want to use, click on OK. You will notice that the black box in your vector parameter node has now turned red.

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

To help ourselves remember what parameter or property of the material our vector parameter will be defining, we should name it color. You can do this by ensuring that you have the vector parameter node selected (indicated by a thin yellow highlight around the node), and then navigating to the Details panel in the editor. Enter a value for Parameter Name, and the node label will change automatically:

The final step is to link our color vector parameter node to the base material node. With Blueprints, you can connect two nodes by clicking and dragging one output pin to another node's input pin. Input pins are located on the left-hand side of a node, while output pins are always located to the right. The thin line that connects two nodes that have been connected in this way is called a wire. For our material, we need to click and drag a wire from the top output pin of the color node to the Base Color input pin of the material node, as shown in the following screenshot:

Adding substance to our material

We can optionally add some polish to our material by taking advantage of some of the other input pins on the material definition node. 3D objects look unrealistic with flat, single color materials applied, but we can add additional reflectiveness and depth by setting a value for the materials Metallic and Roughness inputs. To do so, right click in empty grid space and type scalar into the search box. The node we are looking for is called ScalarParameter.

Once you have a scalar parameter node, select it, and go to the Details panel. A scalar parameter takes a single float value (a number with decimal values). Set Default Value to 0.1, as we want any additive effects to our material to be subtle. We should also change Parameter Name to Metallic. Finally, we click and drag the output pin from our Metallic node to the Metallic input pin of the material definition node.

We want to make an additional connection to the Roughness parameter, so right-click on the Metallic node we just created and select Duplicate. This will generate a copy of that node, without the wire connection. Select this duplicate Metallic node and then change the Parameter Name field in the Details panel to Roughness. We will keep the same default value of 0.1 for this node. Now click and drag the output pin from the Roughness node to the Roughness input pin of the Material definition node.

The final result of our Material Blueprint should look like what is shown in the following screenshot:

We have now made a shiny red material. It will ensure that our targets will stand out when they are hit. Click on the Save button in the top-left corner of the editor to save the asset, and click again on the tab labeled FirstPersonExampleMap to return to your level.

 

Creating our first Blueprint


We now have a cylinder in the world, and the material we would like to apply to the cylinder when shot. The final piece of the interaction will be the game logic that evaluates that the cylinder has been hit, and then changes the material on the cylinder to our new red material. In order to create this behavior and add it to our cylinder, we will have to create a Blueprint. There are multiple ways of creating a Blueprint, but to save a couple of steps, we can create the Blueprint and directly attach it to the cylinder we created in a single click. To do so, make sure you have the CylinderTarget object selected in the Scene Outliner panel, and click on the blue Blueprint/Add Script button at the top of the Details panel. You will then see a path select window.

For this project, we will be storing all our Blueprints in the Blueprints folder, inside the FirstPersonBP folder. Since this is the Blueprint for our CylinderTarget actor, leaving the name of the Blueprint as the default, CylinderTarget_Blueprint, is appropriate.

CylinderTarget_Blueprint should now appear in your content browser, inside the Blueprints folder. Double-click on this Blueprint to open a new editor tab for the Blueprint. We will now be looking at the Viewport view of our cylinder. From here, we can manipulate some of the default properties of our actor, or add more components, each of which can contain their own logic to make the actor more complex. We will explore components more in the next chapter; for now, we want to create a simple Blueprint attached to the actor directly. To do so, click on the tab labeled Event Graph above the Viewport window.

Exploring the Event Graph panel

The Event Graph panel should look very familiar, as it shares most of the same visual and functional elements as the Material Editor we used earlier. By default, the event graph opens with three unlinked event nodes that are currently unused. An event refers to some action in the game that acts as a trigger for a Blueprint to do something. Most of the Blueprints you will create follow this structure: Event (when) | Conditionals (if) | Actions (do). This can be worded as follows: when something happens, check whether X, Y, and Z are true, and if so, do this sequence of actions. A real-world example of this might be a Blueprint that determines whether or not I have fired a gun. The flow is like this: WHEN the trigger is pulled, IF there is ammo left in the gun, DO fire the gun.

The three event nodes that are present in our graph by default are three of the most commonly used event triggers. Event Begin Play triggers actions when the player first begins playing the game. Event Actor Begin Overlap triggers actions when another actor begins touching or overlapping the space containing the existing actor controlled by the Blueprint. Event Tick triggers attached actions every time a new frame of visual content is displayed on the screen during gameplay. The number of frames that are shown on the screen within a second will vary depending on the power of the computer running the game, and this will in turn affect how often Event Tick triggers the actions.

We want to trigger a "change material" action on our target every time a projectile hits it. While we could do this by utilizing the Event Actor Begin Overlap node to detect when a projectile object was overlapping with the cylinder mesh of our target, we will simplify things by detecting only when another actor has hit our target actor. Let's start with a clean slate, by clicking and dragging a selection box over all the default events and hitting the Delete key on the keyboard to delete them.

Detecting a hit

To create our hit detection event, right-click on empty graph space and type hit in the search box. The Event Hit node is what we are looking for, so select it when it appears in the search results. Event Hit triggers actions every time another actor hits the actor controlled by this Blueprint.

Once you have the Event Hit node on the graph, you will notice that Event Hit has a number of multicolored output pins originating from it. The first thing to notice is the white triangle pin that is in the top-right corner of the node. This is the execution pin, which determines the next action to be taken in a sequence. Linking the execution pins of different nodes together enables the basic functionality of all Blueprints. Now that we have the trigger, we need to find an action that will enable us to change the material of an actor. Click and drag a wire from the execution pin to empty space to the right of the node.

Dropping a wire into empty space like this will generate a search window, allowing you to create a node and attach it to the pin you are dragging from in a single operation. In the search window that appears, make sure that the Context Sensitive box is checked. This will limit the results in the search window to only those nodes that can actually be attached to the pin you dragged to generate the search window. With Context Sensitive checked, type set material in the search box. The node we want to select is called Set Material (StaticMeshComponent).

Tip

If you cannot find the node you are searching for in the context-sensitive search, try unchecking Context Sensitive to find it from the complete list of node options. Even if the node is not found in the context-sensitive search, there is still a possibility that the node can be used in conjunction with the node you are attempting to attach it to.

The actions in the Event Hit node can be set like this:

Swapping a material

Once you have placed the Set Material node, you will notice that it is already connected via its input execution pin to the Event Hit node's output execution pin. This Blueprint will now fire the Set Material action whenever the Blueprint's actor hits another actor. However, we haven't yet set up the material that will be called when the Set Material action is called. Without setting the material, the action will fire but not produce any observable effect on the cylinder target.

To set the material that will be called, click on the drop-down field labeled Select Asset underneath Material inside the Set Material node. In the asset finder window that appears, type red in the search box to find the TargetRed material we created earlier. Clicking on this asset will attach it to the Material field inside the Set Material node.

We have now done everything we need with this Blueprint to turn the target cylinder red, but before the Blueprint can be saved, it must be compiled. Compiling is the process used to convert the developer-friendly Blueprint language into machine instructions that tell the computer what operations to perform. This is a hands-off process, so we don't need to concern ourselves with it, except to ensure that we always compile our Blueprint scripts after we assemble them. To do so, hit the Compile button in the top-left corner of the editor toolbar, and then click on Save.

Now that we have set up a basic gameplay interaction, it is wise to test the game to ensure that everything is happening the way we expect. Click on the Play button, and a game window will appear directly above the Blueprint Editor. Try both shooting and running into the CylinderTarget actor you created.

Improving the Blueprint

When we run the game, we see that the cylinder target does change colors upon being hit by a projectile fired from the player's gun. This is the beginning of a framework of gameplay that can be used to get enemies to respond to the player's actions. However, you also might have noticed that the target cylinder changes color even when the player runs into it directly. Remember that we wanted the cylinder target to become red only when hit by a player projectile, and not because of any other object colliding with it. Unforeseen results like this are common whenever scripting is involved, and the best way to avoid them is to check your work by playing the game as you construct it as often as possible.

To fix our Blueprint so that the cylinder target changes color only in response to a player projectile, return to the CylinderTarget_Blueprint tab and look at the Event Hit node again.

The remaining output pins on the Event Hit node are variables that store data about the event that can be passed to other nodes. The color of the pins represents the kind of data variable it passes. Blue pins pass objects, such as actors, whereas red pins contain a Boolean (true or false) variable.

You will learn more about these pin types as we get into more complicated Blueprints; for now, we only need to concern ourselves with the blue output pin labeled Other, which contains the data about which other actor performed the hitting to fire this event. This will be useful in order for us to ensure that the cylinder target changes color only when hit by a projectile fired from the player, rather than changing color because of any other actors that might bump into it.

To ensure that we are only triggering in response to a player projectile hit, click and drag a wire from the Other output pin to empty space. In this search window, type projectile. You should see some results that look similar to the following screenshot. The node we are looking for is called Cast To FirstPersonProjectile:

FirstPersonProjectile is a Blueprint included in Unreal Engine 4's First Person template that controls the behavior of the projectiles that are fired from your gun. This node uses casting to ensure that the action attached to the execution pin of this node occurs only if the actor hitting the cylinder target matches the object referenced by the casting node.

When the node appears, you should already see a blue wire between the Other output pin of the Event Hit node and the Object pin of the casting node. If not, you can generate it manually by clicking and dragging from one pin to the other. You should also remove the connections between the Event Hit and Set Material node execution pins so that the casting node can be linked between them. Removing a wire between two pins can be done by holding down the Alt key and clicking on a pin.

Once you have linked the three nodes, the event graph should look like what is shown in the following screenshot:

Now compile, save, and click on the play button again to test. This time, you should notice that the cylinder target retains its default color when you walk up and touch it, but does turn red when fired upon by your gun.

 

Adding movement


Now that we have a target that responds to the player shooting, we can add some sort of challenge to start making our project feel like a game. A simple way to do this is to add some movement to our target. To accomplish this, we will first have to declare that our target actor is an object that is intended to move, and then we need to set up logic within the Blueprint that will manage how it moves. Our goal will be to make the target cylinder move back and forth across our level.

Changing actor mobility and collision

To allow our target to move, we first have to change the actor's Mobility setting to Moveable. This allows an object to be manipulated while playing the game. From the main editor view, select CylinderTarget_Blueprint, and look at the Details panel. Underneath the Transform values, you can see a toggle for Mobility. Change this from Static to Moveable, as shown in the following screenshot:

Note

By default, basic actors that are placed in the world are set to static. "Static" means that the object cannot move or be manipulated during gameplay. Static objects are significantly less resource intensive to render, and this should be our default choice for non-interactive objects so that we can maximize frame rates.

It is important to note that the version of the target cylinder that we changed in the level is just one instance of the Blueprint template for the target cylinders that we have created. An instance refers to an actual object that has been created, whereas our Blueprints are descriptions of the kind of features that those instances will have once they are created.

Any changes we make to a target cylinder already inside the level will be made for that particular target cylinder only. To make changes to all future targets, we need to modify the Blueprint directly. To do so, open CylinderTarget_Blueprint again, either by navigating to the open tab in the editor, or by double-clicking on the CylinderTarget_Blueprint file in your Blueprints folder.

With the Blueprint open, we want to navigate to the Viewport tab located underneath the menu toolbar. Along the left side, you will see the Components panel, which lists all the components that make up this Blueprint. Since we want to edit a property of the physical object, or mesh, we click on component StaticMeshComponent. You will see a familiar-looking details panel. It includes the same properties and categories that we saw when we edited the target cylinder in the level editing interface. Here, we have to switch the same Mobility toggle, located beneath the Transform properties, from Static to Movable. This will ensure that all future targets created from this Blueprint will already be set to be moveable.

Because we want to target this object with our gun, we also need to ensure that the target is capable of being collided with so that our bullets don't pass through it. Still in the details panel, find the category called Collision and look for Collision Presets in the drop-down menu. There are many other options in this dropdown, and by choosing the Custom option, you can even set the object's collision interaction with different object types individually. For our purpose, we just need to ensure that this drop-down menu is set to BlockAllDynamic, which ensures that the mesh will register collisions with any other object that also has a collider.

Breaking down our goal

Now that we have made our target moveable, we are ready to set up Blueprints that tell the cylinder how to move. In order to move an object, we will need three pieces of data:

  • Where the cylinder currently is

  • What direction it is supposed to move in

  • How fast it is supposed to move in that direction

To understand where the object currently is, we need to get some information about the world itself. Specifically, what are the coordinates of the cylinder in the world? The speed and direction are the values we are going to provide to the Blueprint, though some calculations will be necessary to turn those values into information that is useful for the Blueprint to move the object.

Storing data with variables

The first step is to create the two variables we need: direction and speed. Find the panel labeled My Blueprint. You should see an empty category marker called Variables, with a + sign to the right. Click on that + sign to create your first variable.

In the Details panel, you will see a series of fields for editing your new variable. The four fields that we have to edit are the Variable Name, Variable Type, Editable, and Default Value. We want our first variable to contain information about the speed of movement, so name the variable Speed. For Variable Type, we want a variable that can hold a number that will represent our desired speed, so select Float from the drop-down menu.

Check the box next to Editable to enable the variable to be changed outside of this Blueprint. This will be useful for quickly adjusting the value to our liking once we start testing the moving target in the game. The Default Value category will likely not have a field, but will feature a message asking you to compile the Blueprint first. Do that, and a field for entering an initial value will appear. Change the default value to 200.0.

Using the same process, create a second variable called Direction. Choose Vector for Variable Type. A vector contains information about the X, Y, and Z coordinates, and in this case, we need to indicate the direction of change we want for the object movement. Make the direction variable editable and set Default Value to -10.0 for the Y axis.

Readying direction for calculations

We will now explore the steps necessary to get the information we need to provide a movement instruction. It might look intimidating at first, but we will break down each section and see how each node fits into the larger goal.

The first calculation we need to perform is to take our vector value for direction and normalize it. Normalizing is a common procedure in vector math that ensures that the vector is converted to a length of one unit, which will make it compatible with the rest of our calculations. Fortunately, there is a Blueprint node that takes care of this for us.

Click on the Direction variable we created in the My Blueprint panel, and drag it into empty space in the event graph. A small popup will appear, prompting you to select Get or Set. We want to retrieve the value we set for the direction, so choose Get to create a node containing the direction variable's value. Click on the output pin of the Direction node, and drop it into empty graph space. Type normalize in the search field and select the Normalize node underneath the category labeled Vector. This will connect your Direction variable to a node that will automatically do the normalizing calculation for us.

Note

It is good practice to leave comments on the sets of Blueprints as you create them. Comments can help describe what a particular set of Blueprints is intended to accomplish, which can be helpful if you are returning to a Blueprint after some time and need to make sense of your prior work. To leave a comment on a Blueprint, click and drag a selection box around the nodes you want to create a comment around to select them. Then, right-click on one of the selected nodes and select the bottom option, Create Comment from Selection.

Getting relative speed using delta time

To make our speed value relate to direction, we first need to multiply it by delta time. Delta time is based on the fact that the time taken between the frames of the gameplay can differ. By multiplying our speed value to delta seconds, we can ensure that the speed at which our object moves is the same, regardless of the game's frame rate.

To do this, drag the Speed variable onto the event graph and choose Get to create the speed node. Now, right-click on empty graph space and search for delta. Select the Get World Delta Seconds option to place the corresponding node. Finally, drag the output pin from either the delta seconds node or the speed node, and drop it into empty space. Type an asterisk in the search field (Shift + 8 on most computers) and select the Float * Float node. Finally, drag the other output pin onto the remaining input pin of the new multiplication node to multiply these two values, like this:

Translating the existing location

Now that we have a normalized vector direction and a speed value relative to time, we need to multiply these two values and then add them to the current location. First, find the StaticMeshComponent component from the Components panel and drag it onto the event graph. This will create a node from which we can extract any data contained within the mesh component of the object.

Next, we want to get the mesh's location. One of several ways to handle this is to look at the transform properties of an object and extract the location from there. Click and drag the blue output pin into empty space, and then type Get World. Select the Get World Transform option to create the node. A transform contains information about the rotation and scale of an object, in addition to its location. This will be useful because we want to ensure that we preserve the rotation and scale of our target even as it is moving, and we will need that data to create a transform value from our new movement information.

Now we want to break down the transform into its component parts so that we can use just the location in our calculations, while preserving the rotation and scale. Drag the output pin from the world transform node, and search for the Break Transform node to add to our graph.

Now we need to add the necessary nodes to add speed and direction to the location information we just extracted. Right-click on empty grid space and search and select the Make Transform node. This will mark the end of your calculations, so make sure that it is positioned to the right of all of your other nodes. The Make Transform node has three inputs, Location, Rotation, and Scale. The Rotation and Scale inputs should be connected to the rotation and scale output pins on the Break Transform node we created earlier.

Next, we need to multiply the Direction vector and the Speed float we calculated. Drag the output node of the Normalize node into empty space, and search using an asterisk. Select Vector * Float and connect the green input pin to the output of the float multiplication node that we used with speed.

Our final calculation step is to add Speed and Direction to the current location we calculated. Click on the yellow vector output pin of the new multiplication node, and drag it onto empty space. Search using + and select the Vector + Vector node. Ensure that one input pin of this addition node is connected to the previously mentioned vector multiplication node, and then connect the other input pin is connected to the Location output pin of the Break Transform node. Finally, drag the output pin of our addition node onto the Location input pin of the Make Transform node. When you are finished, the result should look like what is shown in the following screenshot:

Updating location

Now that we have the transform calculated, we can adjust the location of our target actor by this value. We used delta time to make our speed and direction changes consistent across frames, and as a consequence, we can simply use the Event Tick node to fire our move action every frame. Right-click on empty grid space, search for Event Tick, and place the node somewhere to the right of your Make Transform node.

To move the actor, we will be using the Set Actor Transform node. Drag a wire from the execution pin of Event Tick to empty grid space, and search for Set Actor Transform. Place the node, and then connect the Return Value output pin on your Make Transform node to the New Transform input pin on the Set Actor Transform node, as shown here:

 

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.

 

Summary


You have built your first prototype using Unreal 4 Blueprints. Congratulations! The amount of progress you have already made is commendable.

In this chapter, you created a project and an initial level using a first-person shooter template. You then set up a target object that reacts to the player's gunfire by changing appearance. Finally, you set up a Blueprint that will allow you to rapidly create moving targets. The skills you have learned here will serve as a strong foundation for building more complex interactive behavior in later chapters, or even entire games of your own making.

You may wish to spend some additional time modifying your prototype to include a more appealing layout, or feature faster moving targets. As we continue building our game experience, remember that you always have the opportunity to linger on a section and experiment with your own functionality or customizations. One of the greatest benefits of Blueprint's visual scripting is the speed at which you can test new ideas, and each additional skill that you learn will unlock exponentially more possibilities for game experiences that you can explore and prototype.

In the next chapter, we will be looking more closely at the player controller that came with the First Person template. We will extend the existing Blueprint that governs player movement and shooting with a gun that is tweaked to our liking, and produces a more interesting visual impact and sound effects.

About the Author
  • Brenden Sewell

    Brenden Sewell is a game designer and creative director with over a decade of experience leading teams in the development of compelling interactive experiences that entertain and inspire. Prior to joining Bossfight Entertainment to work on some upcoming secret projects, he explored the intersection of social impact and entertainment serving as the Creative Director for E-line Media. He has led developments from concept to live support on a variety of games ranging from a brain-training first-person shooter to a construction sandbox exploring the future of digital fabrication.

    Browse publications by this author
Latest Reviews (3 reviews total)
Simple to buy, good price, good experience.
Great book, all being explained clear and will all necessary resources. Also is easy to understand and know after how to improve the results and how to solve most situations of error.
Blueprints Visual Scripting for Unreal Engine
Unlock this book and the full library FREE for 7 days
Start now