• Advertisement
Sign in to follow this  

AI in Unity: An Abstract Approach?

This topic is 2131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement