≡ Menu

Some Simple AI Behaviors in Unity

Last night I was rewatching one of my favorite movies (Heat), and decided to spend a couple of hours messing around with Unity – I’m a big fan of Unity, but haven’t had a chance to do anything with it since 3.0 dropped.

I wanted a quick project that I could finish before the movie ended, so I decided to throw together a simple ‘game’ that shows a few basic AI behaviors that are frequently used in many different types of games. Have you ever wanted to create some basic NPC AI for a game, but didn’t know where to start? Hopefully this project will give you some ideas.

There are three basic behaviors implemented here: follow, patrol, and wander:

  • Right from the start there should be a soldier following you around.
  • Head up the road to the left and check out some patrol behaviors.
  • Head up the road to the right and check out some wander behaviors.

Simple! Click the image below to see the project in action.

UnitySimpleAIBehaviors

Download the full Unity project by clicking here.

My hope is that this project will be of use to someone interested in getting started with creating NPCs in games. There’s plenty of room for improvement with the behaviors included here, but I feel like they’re pretty decent starting points for a beginner. If you open up the project, most everything  should be easy enough to figure out. If the code seems jacked up, remember I wrote this while halfway paying attention to Heat – cut me some slack! Feel free to drop me a line if you have any questions.

Here are a few additional exercises for the reader if you’re the type who goes for extra credit:

  • There’s currently no sort of pathfinding or navigation going on, but the NPCs do pretty well without it. One major exception is the follow behavior, if you try hard enough you can get the soldier following you to walk through the geometry of the level. Is there anything straightforward you can do to help this problem?
  • Right now soldiers who pause to aim their guns are not actually aiming at anything, just pointing straight ahead. How about extending this behavior to actually point at something? Maybe other NPCs nearby or some geometry or something?
  • On a related note, the soldiers do not currently use headlook – and it might be kind of cool if they did. There’s a sample headlook controller in the Unity Asset Store, and also on the web here.
  • The movement system isn’t very well done. Soldiers don’t actually turn at all, they just snap their rotation to face the next point they’re headed to. This can be improved in a bunch of different ways…
  • Are there any cool ways to combine these behaviors? I’ll bet you can get a halfway decent patrol formation going on by combining the patrol and follow behaviors, and making a few updates.
  • None of these guys attack! You could always make them fire at you, if you’re not into peaceful soldiers.

Feel free to use the included behavior code as the basis for your own project, or to do whatever else you want with it. The visual assets used are from Unity’s Bootcamp demo or free downloads from the Unity Asset Store.

Check out a thread about this project on the Unity forums here.

{ 19 comments… add one }

  • David November 30, 2010, 12:46 pm

    Sweet! This will come in handy for a lot of people who’ve played around with Bootcamp demo and would like to know how to implement simple ai behaviors within the demo. Project shares are always appreciated and very useful for visual learners and N3wbS. :-)

  • TornadoTwins January 21, 2011, 4:46 am

    Excellent work dude, you should post it on UnityPrefabs.com to share it with the (Unity-) world.
    Cheers!
    TT

  • Steven January 21, 2011, 10:42 am

    nice work, thanks for sharing the source code!

  • Kweiko January 27, 2011, 11:49 pm

    Nice!!
    This will indeed come in handy for me, I’ve been looking for something like this for a long time.
    Thank you very much! ^^
    /Kweiko

  • Nick February 20, 2011, 11:13 pm

    Thanks for the code!!!

  • LukeD June 19, 2011, 2:11 pm

    Hey Steve,
    First off thanks a lot for this – it really helped me get to grips with moving objects around automagically in Unity. Incredibly useful intro project! I noticed yesterday that there’s a bug/feature that might not be intended – on line 32 of WanderBehaviour you seem to rely on the current y value being 0, so that when you add the new vector to the current position the y value doesn’t alter – this only works for actors starting at ground level, otherwise they head into space – can 0 be subbed in in place of the current y (it seems to fix the problem) ?

    Thanks again for a great intro!
    Luke

    • Steve June 19, 2011, 8:08 pm

      Hey man, good catch – there’s definitely a bug there.

      When you’re moving the character in WanderBehavior::Update(), if the y value of the vector that we calculate here:

      transform.forward = currentDestination – transform.position;

      … is anything other than 0.0f, the character is going to move either up into the air or down into the terrain. Your solution will work fine in a flat world. Probably better is to match the y value of currentDestination to with the NPC’s transform position y value:

      currentDestination = new Vector3(currentDestination.x, transform.position.y, currentDestination.z);
      transform.forward = currentDestination – transform.position;

      If you start using an approach like this, then you can update that y position to take into account the terrain. If you use Terrain.SampleHeight() then you can set the y value based on that information and send the NPC up/down slopes.

  • Komzinski November 8, 2011, 9:37 am

    hi Steve,
    I have been having troubles on Ai but thanks a lot for the shared codes.

  • Someone April 8, 2012, 10:10 am

    PLEASE DON’T put this on any website that will make us pay for this, you make awesome, helpful stuff and it has helped me with my own games!

    • Steve April 8, 2012, 12:08 pm

      No plans to charge for anything here, don’t worry!

  • dee vaa April 11, 2012, 11:12 am

    Excellent work….thanks for sharing the source code!!!!

  • Microcyb May 5, 2012, 3:20 am

    Excellent work and a huge thank you for teaching us your work. It is always a welcome thing when people learn from each other. I hope to publish works like this as well.

    • Steve May 5, 2012, 2:57 pm

      Glad it was useful!

  • id0 July 21, 2012, 6:00 pm

    Thank you, Steve )

  • Hdalkiran August 16, 2012, 12:23 pm

    many thanks for source

  • L. Christian F. December 20, 2012, 7:18 am

    Thank you so much for the code, the isolated prefabs of the soldier and the ia examples.

    Was very useful for me!

  • killerk1 February 10, 2013, 12:31 pm

    i have a problem with the scripts…
    i have it exactly as you have it but the character with the script disappears
    i was wondering if i did something wrong, because the script was not changed at all or if anyone encountered the same thing.
    Thank you

  • jez August 21, 2013, 8:39 am

    I have fixed a problem in the wander script.

    I posted before saying i could only use this script, not the project and my zombie was floating in the air…

    On line 71 of wanderbehaviour.cs i changed the value of height from;-
    Terrain.activeTerrain.SampleHeight(transform.position)

    to
    Terrain.activeTerrain.SampleHeight(transform.position) + Terrain.activeTerrain.transform.position.y;

    This is to cope with the terrain being higher or lower than world origin.
    (my terrain is an island with water around it).

    I hope this is useful

  • Mauri November 10, 2013, 6:18 pm

    Thanks! You are amazing! This will help me and my team work on an assignment we have! :D Cheers,

Leave a Comment