
Artificial intelligence has become ubiquitous in today’s modern age with the wide variety of video games, electronic gadgets, and apps that try to target ads based on your personal preferences. When it comes to video games a good AI for Non-Player Characters (NPCs) can make or break the game. (See Aliens: Colonial Marines; how a typo broke the AI)
One of the hardest parts of creating a “good” AI is actually making a bit more, well… unintelligent. In programming its actually pretty easy to create AI that has perfect accuracy, perfect precision, and perfect reaction time. The computer can know where all the items are, where the weapons are, where the human players are at any time. Unfortunately its not fun to play against an NPC that can instantly run to every good item and beat the player in a matter of minutes or even seconds.
So then how do we make the AI more… dumb? In Unity we can create senses for the AI and then only allow it to know what those senses detect. In the image above the two “Trolls” from Early Access Survival are using a method from Unity’s physics engine called raycasting. Each AI casts a ray, or shoots (casts) a straight line (ray) directly in front of itself. The method can detect other objects in the scene that have a physics component called a collider. The collider is essentially a simple shape like a box, a sphere, or a rough approximation of the character’s shape, that reacts to physical objects but doesn’t necessarily follow all the physics attributes like gravity. So when one of the “Trolls” throws out a ray in front of itself and that ray collides with another object then the raycast method returns what that object is. Using that data we have logic in the AI’s class that determines what to do. If that object it now “see’s” is an enemy maybe it should attack. If the object is a collectible item maybe it’ll go pick that up. What if its a high level human player? Maybe it should run away instead.
Having the AI sense items in this way rather than knowing exactly where everything is helps make the NPCs seem more realistic. It allows players to have a more enjoyable experience but we can still modify the difficulty to provide an adequate challenge. Is the game too easy? Lets make the AI detect the things from a bit further away. AI to difficult? Maybe we will make the AI run away more often when it detects the player instead of always attacking. All in all creating Artificial Intelligence isn’t the hard part of programming. Creating “good” Artificial Intelligence that makes for an enjoyable experience is the challenging part.

0 comments on “Adding A.I.”