Chapter 10. Behavior, Simulation, and AI
The appearance of intelligent, decision-based behavior is a core facet of building an engaging world or game. Video game artificial intelligence isn’t like the rest of the AI field. It’s mostly fakery, tricks, and illusions of complexity. You don’t need to be an AI or machine learning wizard to build complex, engaging behaviors into video games. In this chapter, we’ll explore solutions to some of the most common problems game developers encounter when giving entities behavior and AI.
We’ll use two different techniques: behavior programming and machine learning.
Note
You can use Unity to work with genuine AI and machine learning! The last few recipes in this chapter explore using Unity ML-Agents, but it’s a big topic. To learn more, check out our book Practical Simulations for Machine Learning (O’Reilly).
10.1 Enemies Detecting When They Can See the Player
Problem
You want an object to be able to detect when it can “see” its target; that is, the target is both close enough to the object and in front of the object. You want to be able to visualize and configure the area in which targets can be seen.
Tip
Obviously, the most common use for this technique is making sure enemies can find the player they need to attack!
Solution
This solution starts with a script:
-
Create a new script called EnemyVisibility.cs, and add the following code to it:
#if UNITY_EDITOR
using
UnityEditor
;
#endif
// Detects when a given target is visible to this ...
Get Unity Development Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.