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

Starting Your AI Journey: Building Intelligent Enemies for Your Game

What is a game if not a great challenge to the player, who needs to use their character’s abilities to tackle different scenarios? Each game imposes different kinds of obstacles for the player, but in our game, the primary challenge comes from the enemies. Imagine an enemy that can sense your presence and strategically plan its attack – this is what we aim to achieve through these AI techniques. Creating challenging and believable enemies can be complex; they must behave like real characters, smart enough to present challenges yet not so formidable as to be invincible. We are going to use basic but sufficient AI techniques to make an AI capable of sensing its surroundings and, based on that information, making decisions on what to do, using Finite State Machines (FSMs), along with other techniques. Those decisions will be executed using intelligent pathfinding.

In this chapter, we will examine the...

Gathering information with sensors

AI in games works in a three-step process: gathering information, analyzing it, and executing actions based on such analysis. This is important as it reflects a simple version of how the human mind works, making the resulting AI more realistic. As you can see, we cannot do anything without information, so let’s start with that part.

There are several sources of information our AI can use, such as data about itself (life and bullets) or maybe some game state (winning condition or remaining enemies), which can easily be found with the code we’ve seen so far. One important source of information, however, is sensors such as sight and hearing. In our case, sight will be enough, so let’s learn how to code that.

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

  • Creating three-filter sensors with C#
  • Creating three-filter sensors with Visual Scripting
  • Debugging with gizmos

Let’...

Creating transitions

If you remember the transitions created in the Animator Controller, those were basically a collection of conditions that checked whether the state the transition belongs to is active. In our FSM approach, this translates simply as if sentences that detect conditions inside the states. Let’s create the transitions between our proposed states as follows:

  1. Add a field of the Sight type called sightSensor in our FSM script, and drag the AI GameObject to that field to connect it to the Sight component there. As the FSM component is in the same object as Sight, we can also use GetComponent instead, but in advanced AIs, you might have different sensors that detect different objects, so I prefer to prepare my script for that scenario. You should pick the approach you like the most.
  2. In the GoToBase function, check whether the detected object of the Sight component is not null, meaning that something is inside our line of vision. If our AI is going...

Calculating our scene’s NavMesh

Pathfinding algorithms rely on simplified versions of the scene. Analyzing the full geometry of a complex scene is almost impossible to do in real time. There are several ways to represent Pathfinding information extracted from a scene, such as graphs and NavMesh geometries. Unity uses the latter – a simplified mesh similar to a 3D model that spans all areas that Unity determines are walkable. In the next screenshot, you can find an example of NavMesh generated in a scene – that is, the light blue geometry:

Figure 9.54: NavMesh of walkable areas in the scene

Generating NavMesh can take from seconds to minutes depending on the size of the scene. That’s why Unity’s Pathfinding system calculates the NavMesh once in the Editor, so when we distribute our game, the user will use the pre-generated NavMesh. In previous Unity versions, like lightmapping, NavMesh used to be baked into a file for later use. That...

Summary

I’m pretty sure AI is not what you imagined; you are not creating Skynet here, but we have accomplished a simple but interesting AI to challenge our players, which we can iterate and tweak to tailor to our game’s expected behavior. We saw how to gather our surrounding information through sensors to make decisions on what action to execute using FSMs and different Unity systems such as Pathfinding to make the AI execute those actions. We used those systems to diagram a state machine capable of detecting the player, running to them, and attacking them, and if the player is not there, just going to the base to accomplish its task to destroy it.

As we move on to the next chapter, we’ll shift our focus to another vital aspect of game development: enhancing the graphics and audio. Get ready to dive into creating materials and shaders that will bring your game world to life.

Learn more on Discord

Read this book alongside other users, Unity game development experts, and the author himself. Ask questions, provide solutions to other readers, chat with the author via Ask Me Anything sessions, and much more. Scan the QR code or visit the link to join the community:

https://packt.link/unitydev

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}