Reader small image

You're reading from  Unreal Engine 5 Game Development with C++ Scripting

Product typeBook
Published inAug 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781804613931
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
ZHENYU GEORGE LI
ZHENYU GEORGE LI
author image
ZHENYU GEORGE LI

Zhenyu George Li is a passionate video game developer with 20+ years of experience. He has contributed significantly to many games and serves as a senior development consultant at Unity. His early immersion in technologies like C/C++, DirectX, OpenGL, and Windows GUI laid the foundation for his success. Notable titles in his portfolio include Magic Arena, Halo Infinity, Stela, Dead Rising 2, and The Bigs 2. He has gained extensive knowledge in programming, graphics, animation, gameplay, AI, multiplayer, and UI using Unreal and Unity engines. Additionally, he has taught UE at Vancouver Film School and has college teaching experience at College of Interactive Arts and Hefei Union University.
Read more about ZHENYU GEORGE LI

Right arrow

Handling Collisions

Collision detection is a useful and effective mechanism to deal with interactions between game actors. The basic idea is that when one actor collides with another actor, a collision event is triggered to notify the two actors about the collision. The game developer can write a script to handle the events and perform the corresponding processes.

In this chapter, we will first introduce you to the basics of the collision detection system, the types of collision components, the collision events, and some of the collision presets that we will use for the development of Pangaea.

Throughout this learning process, you will acquire several skills. This includes adding colliders to actors and meshes to enable accurate collision detection, configuring collision presets, handling overlap events for weapon pickup, and casting rays to perform projectile hit checks.

To demonstrate the usage of the robust mathematical tool FVector, we will focus on simulating the trajectory...

Understanding collision detection

Game collision detection is a system to detect when two or more game objects overlap or interact with each other in a game world. The process of collision detection is based on mathematical calculations that check whether two shapes intersect with each other (overlap detection) or whether a line goes through a surface (ray casting detection).

To detect actor collisions, Unreal offers certain types of simple collision components that use simple shapes to deal with collision detections:

  • UCapsuleComponent
  • UBoxComponent
  • USphereComponent
Figure 8.1 – Simple shape collision components

Figure 8.1 – Simple shape collision components

Besides using the collision components, another option to add simple collision shapes to static meshes is to add them via the Mesh Editor. Double-click on any mesh (model) on the Content Drawer to open the Mesh Editor. You should find the Collision menu item in the Blueprint Editor menu or on the toolbar:

...

Setting the collision presets

Open an actor in the Actor Editor and find the Collision Presets section in the Details panel (see Figure 8.6). The collision presets property of a collidable component has only one drop-down box, which lists the optional collision presets. The settings of a selected preset can be expanded and viewed by clicking the Play button to the left of the Collision Presets label in the actor’s Details tab panel.

The drop-down box has a number of options that you can choose from, but we will just introduce a few of them here (for more information, you can go to the official document site: https://docs.unrealengine.com/5.0/en-US/collision-response-reference-in-unreal-engine/):

  • Custom: All the collision settings can be freely set by the developer.
  • NoCollision: This collider has no collision.
  • BlockAll: This collider blocks all actors in the scene. If a moving actor collides with this actor, the OnActorHit and OnComponentHit events are triggered...

Using collisions for game interactions

To make it fun to play the game, we want to add the following gameplay features:

  • Two types of weapons (sword and axe) can be placed in the game level. The player should walk through a weapon to pick it up. If the player character already has a weapon, when they pick up another weapon, the old weapon is dropped.
  • When an enemy is activated, spawn a hammer weapon for the enemy.
  • Defense towers should be placed in the game level. If the player character enters and stays within the attack range of the tower, the tower fires at the player character by spawning fireballs.
  • A fireball moves along its firing direction and checks whether it hits the target. If the target is hit, it deals damage and destroys itself. Otherwise, the fireball flies for three seconds and destroys itself.

Before we start working on the game, we need to import the weapon and defense tower assets.

Downloading and creating the weapon, defense tower, and...

Summary

In this chapter, you learned about collision detection and how to use the engine’s collision system to bring interactions into the Pangaea game.

The first part of this chapter explained the three collision shape components (CapsuleComponent, BoxComponent, and SpherComponent) as well as the two mesh components (StaticMeshComponent and SekeltalMeshComponet). Adding collision shape components to actors and disabling the mesh collisions for better performance was also explained.

After that, you learned about the OnBeginOverlap and OnEndOverlap collision events and how they work with your C++ code. It was emphasized that you can handle collision events on either an actor or a component, depending on your actual needs.

Appropriate collision presets are vital to gameplay to properly process interactions between actors. It is important to understand what collision preset type, such as BlockAll, OverlapAllDynamic, and NoCollision, should be used for different collision...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unreal Engine 5 Game Development with C++ Scripting
Published in: Aug 2023Publisher: PacktISBN-13: 9781804613931
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 AU $19.99/month. Cancel anytime

Author (1)

author image
ZHENYU GEORGE LI

Zhenyu George Li is a passionate video game developer with 20+ years of experience. He has contributed significantly to many games and serves as a senior development consultant at Unity. His early immersion in technologies like C/C++, DirectX, OpenGL, and Windows GUI laid the foundation for his success. Notable titles in his portfolio include Magic Arena, Halo Infinity, Stela, Dead Rising 2, and The Bigs 2. He has gained extensive knowledge in programming, graphics, animation, gameplay, AI, multiplayer, and UI using Unreal and Unity engines. Additionally, he has taught UE at Vancouver Film School and has college teaching experience at College of Interactive Arts and Hefei Union University.
Read more about ZHENYU GEORGE LI