AI in Unity: An Abstract Approach?

Started by
2 comments, last by lmbarns 12 years ago
NOT ANOTHER "PLEASE GIVE ME AN AI TUTORIAL POST"

(mostly)

I am designing a game in Unity 3d, and this is a question to all the designers out there that have also chosen this development tool specifically (although I'd appreciate help from anyone).

I've never done AI for enemies before. I get the basic concepts though, and I think I could learn the more advanced algorithms for pathfinding and yadda yadda with enough study of the subject, since there is a LOT of documentation about it. My question is more like: what are the best practices for approaching character AI in a game, especially if you're using Unity?

Because of the way Unity works, I more or less want to create a "Behavior" object to attach to any character using AI. This would basically be a script or whatever that would behave a lot like a Player controller, but instead of taking conroller input, it would obviously act based off pre-programmed behaviors. Now obviously, not every character will behave the same way. Some enemies will be very stupid and wander aimlessly, others will try to more directly attack the player, and some might even be ally characters who need to respond to the player in distress.

What I want to do is have it so that every instance of this "Behavior", I guess you'd call it, "class" attached to these characters to be loadable with specific behaviors. That way I could have a modular list of behaviors that can be added and subtracted from a list and greatly change the way an AI controlled character will behave.

My question is this best practice? If not what is? And if so, what would be the best way to go about this in Unity so that it is as modular as possible to allow for high customization?

This is sort of an abstract question, and therefore only really expect abstract results unless if someone can point me to a tutorial where maybe this sort of thing is demonstrated. Thanks!

AniMerrill, a.k.a. Ethan Merrill

Advertisement
I'm also using Unity, and currently working on my game's AI. It's a space game, and each ship has a controller behavior. It contains the methods that allow for basic movement such as set throttle forward, or turn to the right. For the player, it listens to input and reacts accordingly. For ai, it doesn't listen to input, and a separate AI behavior calls the movement methods directly. I plan on making the AI contained in a single script, with modifiable variables and settings represented by enums to vary behavior. I'm not sure how using separate behaviors would work, unless all they did was modify variables in the master AI script. Unless you're talking about AI with completely different behavior (e.g. turret vs pac man ghost), then it would seem you just need a completely different script for each one.

I'm not really sure what "best practice" would be in this case, though. I imagine your exact needs really change what the best solution is.
You could also have a base class that does a lot of the mundane stuff that other classes inherit from (in addition to the Unity Behavior class). Then, each type of character could be assigned the script appropriate to its specific needs. However, if there is similarity between then, the enum + parameterized approach works well, too.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

I start with a basic state machine, which I got from the hack n slash tutorials, called AI, and add additional states for all the activities I want.


using UnityEngine;
using System.Collections;
[RequireComponent (typeof(AdvancedMovement))]
[RequireComponent (typeof(SphereCollider))]
public class AI : MonoBehaviour {
private enum State {
Idle, //do nothing
Init, //make sure that everything we need is here
Setup, //assign the values to the things we need
Search, //find the player
Attack, //attack the player
Retreat, //retreat to spawn point
Flee //run to the nearest spawn point with another mob
}


It requires my character controller script and a sphere collider to check a perception radius. If the player enters the perception radius, the state machine starts searching for a target and moving towards it. If it gets close enough, it enters an attack state which triggers an attack function. You can get as complex as you want within the state. Your move() function can use whatever pathfinding you want, a*, or basic trig.

I'd check out the actual tutorial: http://www.burgzerga...engine-tutorial tutorials 103-105 are the basic movement and ai if the player enters the perception radius, but runs too far away, the target reverts back to the spawn point, if the player re-enters the perception radius it becomes the target. For the flee you have to store the location of other spawn points.

This topic is closed to new replies.

Advertisement