Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hands-On Unity  Game Development - Fourth Edition

You're reading from  Hands-On Unity Game Development - Fourth Edition

Product type Book
Published in Jan 2024
Publisher Packt
ISBN-13 9781835085714
Pages 742 pages
Edition 4th Edition
Languages
Authors (2):
Nicolas Alejandro Borromeo Nicolas Alejandro Borromeo
Profile icon Nicolas Alejandro Borromeo
Juan Gabriel Gomila Salas Juan Gabriel Gomila Salas
Profile icon Juan Gabriel Gomila Salas
View More author details

Table of Contents (28) Chapters

Preface 1. Section 1: Getting Started with Unity
2. Embark on Your Unity Journey 3. Crafting Scenes and Game Elements 4. From Blueprint to Reality: Building with Terrain and ProBuilder 5. Seamless Integration: Importing and Integrating Assets 6. Section 2: Mastering Programming and Gameplay Mechanics
7. Unleashing the Power of C# and Visual Scripting 8. Dynamic Motion: Implementing Movement and Spawning 9. Collisions and Health: Detecting Collisions Accurately 10. Victory or Defeat: Win and Lose Conditions 11. Starting Your AI Journey: Building Intelligent Enemies for Your Game 12. Section 3: Elevating Visuals, Effects, and Audio
13. Material Alchemy: Using URP and Shader Graph for Stunning Visuals 14. Captivating Visual Effects: Harnessing Particle Systems and Visual Effect Graph 15. Enlightening Worlds: Illuminating Scenes with the Universal Render Pipeline 16. Immersive Realism: Achieving Fullscreen Effects with Post-Processing 17. Harmonious Soundscapes: Integrating Audio and Music 18. Section 4: Designing User Interfaces, Animations and Advanced Concepts
19. Interface Brilliance: Designing User-Friendly UI 20. Next-Gen UI: Creating Dynamic Interfaces with UI Toolkit 21. Animated Realities: Creating Animations with Animator, Cinemachine, and Timeline 22. Performance Wizardry: Optimizing Your Game with Profiler Tools 23. From Prototype to Executable: Generating and Debugging Your Game 24. AR/VR 25. Massive Worlds: Introduction to DOTS 26. Other Books You May Enjoy
27. Index

Collisions and Health: Detecting Collisions Accurately

As games try to simulate real-world behaviors, one important aspect of simulating is physics, which dictates how objects move and how they collide with each other, such as the collision of players and walls or bullets and enemies. Physics can be difficult to control due to the myriad of reactions that can happen after a collision, so we will learn how to properly configure our game to create physics as accurately as we can. This will generate the desired arcade movement feeling based on realistic collisions—after all, sometimes, real life is not as interesting as video games!

In this chapter, we will examine the following collision concepts:

  • Configuring physics
  • Detecting collisions
  • Moving with physics

First, we will learn how to properly configure physics, a step needed for the collisions between objects to be detected by our scripts, using new Unity events that we will learn. All of...

Configuring physics

Unity’s physics system is prepared to cover a great range of possible gameplay applications, so properly configuring it is important to get the desired result. In this section, we will examine the following physics settings concepts:

  • Setting shapes
  • Physics object types
  • Filtering collisions

We are going to start by learning about the different kinds of colliders that Unity offers and then learn about different ways to configure those to detect different kinds of physics reactions (collisions and triggers). Finally, we will discuss how to ignore collisions between specific objects to prevent situations such as the player’s bullets damaging the player.

Setting shapes

At the beginning of this book, we learned that objects usually have two shapes: the visual shape (which is basically the 3D mesh) and the physical one (the collider—the one that the physics system will use to calculate collisions). Remember...

Detecting collisions

As you can see, proper physics settings can be complicated and very important, but now that we have tackled that, let’s perform some tasks with those settings by reacting to the contact in different ways, creating a health system in the process.

In this section, we will examine the following collision concepts:

  • Detecting Trigger events
  • Modifying the other object

First, we are going to explore the different collision and trigger events Unity offers us to react to contact between two objects, through Unity collision events. This allows us to execute any reaction code we want to place, but here, we are going to explore how to modify the contacted object components using the GetComponent function.

Detecting Trigger events

If objects are properly configured, as previously discussed, we can get two reactions: collisions or triggers. The Collision reaction has a default effect that blocks the movement of the objects, but we...

Moving with physics

So far, the player, the only object that moves with the Dynamic Collider Profile and the one that will move with physics, actually moves through custom scripting using the Transform API. Instead, every dynamic object should move using the Rigidbody API functions in a way the physics system understands better. As such, here, we will explore how to move objects, this time through the Rigidbody component.

In this section, we will examine the following physics movement concepts:

  • Applying forces
  • Tweaking physics

We will start by seeing how to move objects the correct physical way through forces, and we will apply this concept to the movement of our player. Then, we will explore why real physics is not always fun and how we can tweak the physics properties of our objects to have a more responsive and appealing behavior.

Applying forces

The physically accurate way of moving an object is through forces, which affect the object’...

Summary

Every game has physics, in some way or another, for movement, collision detection, or both. In this chapter, we learned how to use the physics system for both being aware of proper settings to make the system work properly, reacting to collisions to generate gameplay systems, and moving the player in such a way that it collides with obstacles, keeping its physically inaccurate movement. We used these concepts to create our player and bullet movement and make our bullets damage the enemies, but we can reuse this knowledge to create a myriad of other possible gameplay requirements, so I suggest you play around a little bit with the physics concepts seen here; you can discover a lot of interesting use cases.

In the next chapter, we will discuss how to program the visual aspects of the game, such as effects, and make the UI react to the input.

Learn more on Discord

Read this book alongside other users, Unity game development experts, and the author himself. Ask questions...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Hands-On Unity Game Development - Fourth Edition
Published in: Jan 2024 Publisher: Packt ISBN-13: 9781835085714
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 AU $19.99/month. Cancel anytime}