AI Unity game for dummies

Mohamed Derkaoui
Frianbiz
Published in
5 min readJul 10, 2019

YO ! I’m Mohamed, a software engineer @Frianbiz, and I’m about to show you our AI based game made with Unity.

For more context, I’m glad to announce that it’s my first interaction with this game engine. So if you’re a beginner too, I’m here to share with you the very basics to use this great tool. No strong C# or design skills are required, but a programming background will surely help.

  1. Unity, quésaco 🤷🏻‍♂️ ?
  2. Storytelling 🧚🏻‍♀️
  3. Dev and AI part 🤖
  4. The moment of truth 🥁

Unity, quésaco 🤷🏻‍♂️ ?

Unity is a widly known cross-platform game engine, first announced and released as a Mac OS X-exclusive game engine. As of 2018, it has been extended to support more than 25 platforms. It can be used to create 2D, 3D, VR and AR games, as well as many other experiences.

For our tutorial, we’ll use it to craft a small 3D game. Therefore, an introduction to the basic components is a must. For the sake of simplicity, we won’t dive into the details. But if you’re curious to know more, I invite you to grab a cup of coffee and follow me here.

GameObjects

Different GameObjects made with Unity — source : Unity

Every kind of content in Unity begins with a GameObject. Any object in your game is a GameObject: characters, lights, special effects, props–everything. GameObjects can’t do anything on their own. To actually become something, you need to give a GameObject properties, which you do by adding Components.

Components

Components example for a single GameObject — source : Unity

Components define and control the behaviour of GameObjects they are attached to. A simple example would be the creation of a light, which involves attaching a Light Component to a GameObject (see above).

Variables

Components have any number of editable properties that can be tweaked via the Inspector window in the editor, and/or via script. In the above example, some properties of the light are range, color and intensity.

Behaviour

Unity’s built-in Components are very versatile, but soon you will find out that you need to go beyond what they can provide to implement your own gameplay logic. Using scripts, you can implement your own game logic and behaviour by simply applying them to the game objects.

Your script Components will allow you to do many things: trigger game events, check for collisions, apply physics, respond to user input, and much more. 👨🏻‍🔬 🧬🧪

Storytelling 🧚🏻‍♀️

Photo by Nong Vang on Unsplash

Once upon a time in the west, there was a small village near a wild forest. Its inhabitants were known not only for their bravery and intelligence, but also their strength. No story without drama 😩. Our beloved villagers are out of wood supply, and WINTER IS COMING.

Unfortunately, they can’t control the weather yet, but they have the superpower of switching jobs in the blink of an eye! Isn’t it cool ?! A carpenter can easily become a black-smith, a painter or even a woodcutter !!

Got it ? Yes !! They decided to be lumberjacks 🌲 so they can save their lives and their village as well. Till now everything is perfect except that they don’t know how many lumberjacks they need? How much wood can a single one pick ? How much time they still got ? And who can manage the whole process ?!

I would’ve loved to help, but I’m not that good 😞.There are many “states” and constraints to manage and there’s not much I can do. Therefore, my colleagues and I decided to develop an Artificial Intelligence algorithm for this noble purpose.

Dev and AI part 🤖

First things first !

As any other project, we need to define our classes, their properties and methods. And again, to make things look smooth, we will present to you the very basic and easily understood form. A Github repo is provided here to take a look at all the scripts and the detailed game logic.

Excerpt from Lumberjack class
Excertp from Forest class

That’s it for our classes. Sure there’s more to discover on the repo, but all I want you to have here is a general idea about how it’s baked behind the scenes. And I guess you did 🤓.

AI, aka if elseif 🧠

Like the code above, the AI algorithm is not hard to grasp too😊. It is a finite state AI and, as you might have guessed, the states here depend on the constraints that our villagers face : can he transform into a lumberjack ? Can he pick more wood ? When could he return to his normal state? …

Here are some of the constraints and rules used :

- The  Villager should go to the Sawmill at the Start of the game,             so they can convert to Lumberjack ⚠️ Warning: There can only be 10  Lumberjacks- The Villager should take wood from the Storage to the House- The Lumberjack should go to the Forest when the game Starts- The Lumberjacks should walk to their assigned  Tree once they are in the Forest

In code, the states and their adequate actions are written as below

Excerpt from the Lumberjack class logic

To dive deeper into this finite state AI subject, I suggest a very useful article.

The moment of truth 🥁

Long story short, here’s a demo of our game 🤩. Hope you like it !

Game Demo

Thaaat’s it ! As we’ve seen, Unity is a great tool to build and prototype games. If it’s a subject of interest to you, I recommend digging into lower level tutorials.

To integrate AI or not ? To use finite state AI or some trained AI models ? ! Start first and you’ll get your answer on the way.

Photo by Alexander Dummer on Unsplash

At last, I’m glade to let you know that what you’re reading now is my FIRST Medium article -after 2 years of procrastinating 🙃-. Because yeah, WHY NOT.

A big shout out to all my colleague @Frianbiz for the opportunity, especially lucas colomer for the elegant work. YO !

--

--

Mohamed Derkaoui
Frianbiz

Technology passionate, Life pupil. I create bugs for a living.