Reader small image

You're reading from  Blueprints Visual Scripting for Unreal Engine 5 - Third Edition

Product typeBook
Published inMay 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781801811583
Edition3rd Edition
Languages
Tools
Right arrow
Authors (2):
Marcos Romero
Marcos Romero
author image
Marcos Romero

Marcos Romero is the author of the Romero Blueprints blog, which is one of the main references on the internet to learn about Blueprints. Epic Games invited Marcos to the Unreal Engine 4 closed beta program to experiment and collaborate with the evolution of the tools. He was also one of the first recipients of Unreal Dev Grants for Education. Marcos is a well-known figure in the Unreal community and, for Epic Games, he wrote the official Blueprints Compendium and Blueprints Instructor's Guide.
Read more about Marcos Romero

Brenden Sewell
Brenden Sewell
author image
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.
Read more about Brenden Sewell

View More author details
Right arrow

Chapter 5: 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 5 and Blueprints make it easier than ever to quickly get the essential gameplay functionality working so that you can start testing your ideas sooner. 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
  • Moving objects in the world with Blueprints

By the end of this chapter, we will have learned how to create a Blueprint target that changes its Material when it is hit and moves back and forth between two points regularly. Each instance of the Blueprint target in the Level can be set to different speeds, directions, and times to change direction.

...

Creating the project and the first Level

In this section, we will start by creating a project using one of the Unreal Engine templates. We will then explore the template to see what gameplay elements it provides.

Our game will be a first-person shooter. So, let's create a project using the First Person template, which is in the Games category:

Figure 5.1 – Choosing the First Person template

Below the game templates, you can see a folder path field used to designate where you would like to store your project. You can use the default folder or choose one you prefer. The next screenshot shows the project defaults you should use in this project. These PROJECT DEFAULTS options were explained in Chapter 1, Exploring the Blueprint Editor. There is also a Project Name field to input the name by which your project will be known. I named the project UE5BpBook, as shown in this screenshot:

Figure 5.2 – The project defaults used...

Adding objects to our Level

Now, we want to start adding our own objects to the Level. Our goal is to create a simple target Actor that changes color when shot with the included gun and projectile. We can create a simple Actor by following these steps:

  1. In the Level Editor, click the Create button located on the toolbar. Hover over Shapes to display a submenu and drag Cylinder and drop it somewhere in the Level to create an instance:

Figure 5.3 – Adding a Cylinder shape to the Level

This creates a new Cylinder Actor and places it in our Level. You can reposition the Cylinder as you wish by dragging and dropping it. You should see the Actor in the Viewport as well as in the World Outliner panel, where it is named Cylinder by default:

Figure 5.4 – World Outliner showing the added Cylinder

  1. In the Details panel, change the name of the Cylinder instance to CylinderTarget, as shown here:
...

Exploring Materials

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

Creating Materials

Follow these steps to create a Material:

  1. Click the Content Drawer button to open the content browser, and then click the FirstPersonBP folder to access it. Click the Add button, select New Folder, and name it Materials. This step is not necessary, but it is good practice to keep the project file hierarchy tidy.
  2. Navigate to the newly created folder...

Creating the target Blueprint

We now have a Cylinder in the world, as well as the Material we want to apply to the Cylinder when it is shot. The final piece of the interaction is the game logic that evaluates that the Cylinder has been hit, and then changes the Material on the Cylinder to our new red Material. To create this behavior, we must convert our Cylinder into a Blueprint. To do so, follow these steps:

  1. Make sure you have the CylinderTarget object selected in the Level. In the Details panel, click on the icon on the right of the Add button:

Figure 5.14 – Creating a Blueprint from an Actor in the Level

  1. You will then see a window with the title of Create Blueprint From Selection. Rename the Blueprint BP_CylinderTarget. In the Path field, select the /Game/FirstPersonBP/Blueprints folder. In Creation Method, use the New Subclass option. The StaticMeshActor parent class is already selected because it is the parent class of the Cylinder...

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 first must 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 manages how it moves. Our goal is to make the target Cylinder move back and forth across our Level.

Changing the Actor's Mobility and Collision settings

To allow our target to move, we first must change its Mobility setting to Moveable. This allows an Actor to be manipulated while playing the game. To do this, follow these steps:

Important Information

By default, basic Actors that are placed in the world are set to Static. Static means that an object cannot move or be manipulated during gameplay. Static objects are significantly less resource-intensive to render, and this should be our default...

Changing direction

In this section, we will implement the logic to 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:

  1. Right-click in an empty space of the EventGraph to open the context menu and search for custom event. Select the Add Custom Event option. Rename the event ChangeDirection:

Figure 5.36 – Creating a custom event

  1. We are going to invert the Direction vector by multiplying it by -1:

Figure 5.37 – Invert the direction of a vector by multiplying it by -1

  1. Drag the Direction variable from the My Blueprint panel and drop it into the EventGraph. Choose the Get Direction option to create a node.
  2. Drag from the output pin of the Direction node and drop it into an empty space. Type an asterisk (*) in the search field and select the Multiply node.
  3. The previous step...

Summary

In this chapter, we created a project and an initial Level using a first-person shooter template. We then set up a target object that reacts to the player's gunfire by changing its appearance. Finally, we set up a Blueprint that allows us to rapidly create moving targets. The skills we have learned about 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 can linger on a section and experiment with your own functionality or customization. One of the greatest benefits of a 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...

Quiz

  1. It is not possible to change the Material of a mesh using Blueprint script.

a. True

b. False

  1. You can create a Blueprint class using an Actor that is in the Level.

a. True

b. False

  1. The resulting vector of the Normalize function is a vector with a length equal to 1.

a. True

b. False

  1. The amount of time a timer waits to execute an event is called delta time.

a. True

b. False

  1. Event Tick is executed every frame.

a. True

b. False

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition
Published in: May 2022Publisher: PacktISBN-13: 9781801811583
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Marcos Romero

Marcos Romero is the author of the Romero Blueprints blog, which is one of the main references on the internet to learn about Blueprints. Epic Games invited Marcos to the Unreal Engine 4 closed beta program to experiment and collaborate with the evolution of the tools. He was also one of the first recipients of Unreal Dev Grants for Education. Marcos is a well-known figure in the Unreal community and, for Epic Games, he wrote the official Blueprints Compendium and Blueprints Instructor's Guide.
Read more about Marcos Romero

author image
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.
Read more about Brenden Sewell