Starting with AI

Hey guys

I got to the point when I need to make an AI for my game and I wonder how you start. I mean…like to write some main parts on paper like behavior of an enemy (depends on what it is, for me a spacecraft). Or make some research or something. Just some ways you start. Thanks!

AI is broken up into various parts. Steering, attacking or whatever stuff you will need. A bunny AI, may be concerned with danger, evasion, eating and mating. Spacecraft could also be broken up the same way, but mating may be a little difficult.

As for where to start and how to build it. There are some interesting methods you could use. The “Steering” behaviour could be used to do most everything. It is a method based on components on a game object. All derived from a common base class.

So, lets look at a rabbit.
SteeringBase
Foraging
Evading
Hiding

OK, each of these 3 are basically SteeringBase components (extends SteeringBase) so all you then have to do, to get all of the steering components on your object is to use gameObject.GetComponents(SteeringBase).

Each of them are base components, but also have their own elements. SteeringBase should contain an element that will be common to all of them. (DoSteer)

DoSteer would set a couple of key variables in the base class. (direction, intensity) Direction is of course the direction of movement while intensity is a special vairable for use in all the steering components.

So imagine if you will. Your Rabbit forages. For this, he doesnt move and his direction is Vector3.zero (sitting still foraging at the moment) and this intensity is 1. He does this all the time, no matter what. He also has spotted a predator, so he also gives a Vector3 away from the enemy towards the direction of cover or his home. Since he has spotted a predator, the Evading Steering controller sets the intensity of all other controllers to zero. Thus stopping his foraging. He then starts moving. Now you do some math. Add all of the directions together, each one multiplied by the intensity, then divide them by 3. (foraging * 0 = 0, evading * 1 = 1, hiding * 0 = 0). Now, you move him according to the sum of the directions. Since evading is 1, then it gets all of the movement.

Whenever your bunny can no longer see the predator, it starts dropping the evading by (lets say 3 * Time.deltaTime) Mathf.MoveTowards works perfectly here. The Evading controller takes the remainder and puts it into the Hiding controller. The hiding controllers direction is always Vector3.zero. as it gains in momentum, the rabbit will tend to stay in one place. Once Hiding becomes 1, then it, like the Evading will start dropping, and putting its points into foraging. Thus completing the cycle.

How you accomplish it all, is up to you. A rabbit will dart from place to place in order to let the momentum of a predator carry it past it There may be more complex behaviors that you want to add. All up to you.