Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Game Development Projects with Unreal Engine

You're reading from  Game Development Projects with Unreal Engine

Product type Book
Published in Nov 2020
Publisher Packt
ISBN-13 9781800209220
Pages 822 pages
Edition 1st Edition
Languages
Authors (4):
Hammad Fozi Hammad Fozi
Profile icon Hammad Fozi
Gonçalo Marques Gonçalo Marques
Profile icon Gonçalo Marques
David Pereira David Pereira
Profile icon David Pereira
Devin Sherry Devin Sherry
Profile icon Devin Sherry
View More author details

Table of Contents (19) Chapters

Preface
1. Unreal Engine Introduction 2. Working with Unreal Engine 3. Character Class Components and Blueprint Setup 4. Player Input 5. Line Traces 6. Collision Objects 7. UE4 Utilities 8. User Interfaces 9. Audio-Visual Elements 10. Creating a SuperSideScroller Game 11. Blend Spaces 1D, Key Bindings, and State Machines 12. Animation Blending and Montages 13. Enemy Artificial Intelligence 14. Spawning the Player Projectile 15. Collectibles, Power-Ups, and Pickups 16. Multiplayer Basics 17. Remote Procedure Calls 18. Gameplay Framework Classes in Multiplayer

6. Collision Objects

Overview

In this chapter, we will continue working on the collision-based game we introduced in the previous chapter by adding further mechanics and objects to our game. Initially, we will follow on from the previous chapter by introducing object collision. You will learn how to use collision boxes, collision triggers, overlap events, hit events, and physics simulation. You will also learn how to use timers, the Projectile Movement Component, and Physical Materials.

Introduction

In the previous chapter, we came across some of the basic concepts of collision, namely Line Traces and Sweep Traces. We learned how to execute different types of Line Traces, how to create our own custom Trace Channels, and how to change how an object responds to a specific channel. Many of the things you learned in the previous chapter will be used in this chapter, where we'll learn about object collision.

Throughout this chapter, we will continue to build upon our top-down Dodgeball game by adding game mechanics that revolve around object collision. We will create the Dodgeball actor, which will act as a dodgeball that bounces off of the floor and walls; a Wall actor, which will block all objects; a Ghost Wall actor, which will only block the player, not the enemies' lines of sight or the dodgeball; and a Victory Box actor, which will end the game when the player enters the Victory Box, representing the end of the level.

Before we start creating our...

Object Collision in UE4

Every game development tool must have a physics engine that simulates collision between multiple objects, as explained in the previous chapter. Collision is the backbone of most games released nowadays, whether 2D or 3D. In many games, it's the main way in which the player acts upon the environment, be it running, jumping, or shooting, and the environment acts accordingly by making the player land, get hit, and so on. It is no understatement to say that, without simulated collision, it wouldn't be possible to make many games at all.

So, let's understand how object collision works in UE4 and the ways in which we can use it, starting with collision components.

Collision Components

In UE4, there are two types of components that can affect and be affected by collision; they are as follows:

  • Meshes
  • Shape objects

Meshes can be as simple as a cube, or as complex as a high-resolution character with tens of thousands of vertices. A mesh's collision can be specified with a custom file imported alongside the mesh into UE4 (which is outside the scope of this book), or it can be calculated automatically by UE4 and customized by you.

It is generally a good practice to keep the collision mesh as simple (few triangles) as possible so that the physics engine can efficiently calculate collision at runtime. The types of meshes that can have collision are as follows:

  • Static Meshes
  • Skeletal Meshes
  • Procedural Meshes
  • And so on

Shape objects, which are simple meshes represented in wireframe mode that are used to behave as collision objects by causing and receiving collision events.

Note

Wireframe mode...

Collision Events

Let's say that there are two objects colliding into one another. Two things can happen:

  • They overlap each other, as if the other object weren't there, in which case the Overlap event is called.
  • They collide and prevent each other from continuing their course, in which case the Block event is called.

In the previous chapter, we learned how to change an object's response to a specific Trace channel. During this process, we learned that an object's response can be either Block, Overlap, or Ignore.

Now, let's see what happens in each of these responses during a collision.

Block: Two objects will only block each other if both of them have their response to the other object set to Block:

  • Both objects will have their OnHit events called. This event is called whenever two objects block each other's path at the moment they collide. If one of the objects is simulating physics, that object must have its SimulationGeneratesHitEvents...

Collision Channels

In the previous chapter, we took a look at the existing Trace Channels (Visibility and Camera) and learned how to make our own custom channel. Now that you know about Trace Channels, it's time to talk about Object Channels, also known as Object Types.

While Trace Channels are only used for Line Traces, Object Channels are used for object collision. You can specify a "purpose" for each Object Channel, much like with Trace Channels, such as Pawn, Static Object, Physics Object, Projectile, and so on. You can then specify how you want each Object Type to respond to all the other Object Types by blocking, overlapping, or ignoring objects of that type.

Collision Properties

Now that we've taken a look at how collision works, let's go back to the collision settings of the cube we selected in the previous chapter, where we changed its response to the Visibility Channel.

The cube can be seen in the following screenshot:

Figure 6.4: Cube blocking the SightSource of the enemy

With the level open in the editor, select the cube and go to the Collision section of its Details Panel:

Figure 6.5: The changes in the level editor

Here, we can see some options that are important to us:

  • SimulationGeneratesHitEvents, which allows the OnHit events to be called when an object is simulating physics (we'll talk about this later in this chapter).
  • GenerateOverlapEvents, which allows the OnBeginOverlap and OnEndOverlap events to be called.
  • CanCharacterStepUpOn, which allows a character to easily step up onto this object.
  • CollisionPresets, which allows us to...

Physical Materials

In UE4, the way you can customize how an object behaves while simulating physics is through Physical Materials. In order to get into this new type of asset, let's create our own:

  1. Create a new folder inside the Content folder called Physics.
  2. Right-click on the Content Browser while inside that folder and, under the Create Advanced Asset section, go to the Physics subsection and select Physical Material.
  3. Name this new Physical Material PM_Dodgeball.
  4. Open the asset and take a look at the available options.

Figure 6.13: Asset options

The main options we should note are as follows:

  • Friction: This property goes from 0 to 1 and specifies how much friction will affect this object (0 means this object will slide as if it was on ice, while 1 means this object will stick like a piece of gum).
  • Restitution (also known as Bounciness): This property goes from 0 to 1 and specifies how much velocity will be kept after colliding...

Timers

Given the nature of video games and the fact that they're strongly event-based, every game development tool must have a way for you to cause a delay, or a wait time, before something happens. For instance, when you're playing an online death match game, where your character can die and then respawn, usually, the respawn event doesn't happen the instant your character dies but a few seconds later. There is a multitude of scenarios where you want something to happen, but only after a certain amount of time. This will be the case for our EnemyCharacter, which will be throwing dodge balls every few seconds. This delay, or wait time, can be achieved through timers.

A timer allows you to call a function after a certain amount of time. You can choose to loop that function call with an interval and also set a delay before the loop starts. If you want the Timer to stop, you can also do that.

We will be using timers so that our enemy throws a dodge ball every X amount...

Spawning Actors

In Chapter 1, Unreal Engine Introduction, you learned how to place an actor that you created in the level through the editor, but what if you wanted to place that actor in the level as the game is being played? That's what we're going to be taking a look at now.

UE4, much like most other game development tools, allows you to place an actor in the game while the game itself is running. This process is called spawning. In order to spawn an actor in UE4, we need to call the SpawnActor function, available from the World object (which we can access using the GetWorld function, as mentioned previously). However, the SpawnActor function has a few parameters that need to be passed, as follows:

  • A UClass* property, which lets the function know the class of the object that will be spawned. This property can be a C++ class, available through the NameOfC++Class::StaticClass() function, or a Blueprint class, available through the TSubclassOf property. It...

Walls

The next step in our project is going to be creating the Wall classes. We will have two types of walls:

  • A normal wall, which will block the enemy's line of sight, the player character, and the dodge ball.
  • A ghost wall, which will only block the player character, and ignore the enemy's line of sight and the dodge ball. You might find this type of collision setup in specific types of puzzle games.

We'll create both these Wall classes in the next exercise.

Exercise 6.04: Creating Wall Classes

In this exercise, we will be creating the Wall classes that represent both a normal Wall and a GhostWall, which will only block the player character's movement, but not the enemies' lines of sight or the dodge balls they throw.

Let's start with the normal Wall class. This C++ class will basically be empty because the only thing that it'll need is a mesh in order to reflect the projectiles and block the enemies' lines of sight...

Victory Box

The next step in our project is going to be creating the VictoryBox actor. This actor will be responsible for ending the game when the player character enters it, given that the player has beaten the level. In order to do this, we'll be using the Overlap event. The following exercise will help us understand Victory Box.

Exercise 6.05: Creating the VictoryBox class

In this exercise, we will be creating the VictoryBox class, which, when entered by the player character, will end the game.

The following steps will help you complete this exercise:

  1. Create a new C++ class that inherits from the actor and call it VictoryBox.
  2. Open that class's files in Visual Studio.
  3. Create a new SceneComponent property, which will be used as a RootComponent, just like we did with our Wall C++ class:
    • Header file:
      private:
      UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category =   VictoryBox, meta = (AllowPrivateAccess = "true"))
      class USceneComponent* RootScene...

Summary

In this chapter, you've learned how to affect an object with physics simulations, create your own Object Types and Collision Presets, use the OnHit, OnBeginOverlap, and OnEndOverlap events, update an object's Physical Material, and use timers.

Now that you've learned these fundamental concepts of collision topics, you'll be able to come up with new and creative ways to use them when creating your own projects.

In the next chapter, we'll be taking a look at Actor Components, Interfaces, and Blueprint Function Libraries, which are very useful for keeping your project's complexity manageable and highly modular, thereby allowing you to easily take parts of one project and add them to another.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Game Development Projects with Unreal Engine
Published in: Nov 2020 Publisher: Packt ISBN-13: 9781800209220
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.
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}