Dynamic AI Project Help Page

Important note on help questions: I am very busy with many projects, and I run several threads. However, you are far more likely to get a response by simply sending me a PM right here on the Unity forums. Just specify which package and what your problem is.

Welcome to the Dynamic AI Help Page. This page will give you access to tutorials and downloadable content.

Main Forum Thread: http://forum.unity3d.com/threads/154639-Dynamic-AI-Project

Asset Store Link http://u3d.as/content/brilliant-game-studios/dynamic-navigation-beta-/3FZ

[u]Dynamic Navigation[/u]

Video Tutorial

ATTENTION PEOPLE WITH FANTASY AI!

For those of you who own Fantasy AI, you may know this Navigation system is compatible with Fantasy AI! Just simply download the link below and follow the instructions!

Download Link: https://dl.dropbox.com/u/65183439/Fantasy%20pluggin/FantasyPlugginPlay.cs

Fantasy AI Instructions:

  • First you must make sure both Fantasy AI and Dynamic navigation are imported into your project(This is why this script was not included with the package right off the bat, errors would occur from scripts not existing).

  • Now, import the C# script from the download link(“Fantasy Pluggin Play”).

  • To make the AI use this pathfinding, first remove the old “Navigation” script attached to your Fantasy AI character, as well as the “Navmeshtarget” script.

  • Now add the “Fantasy Pluggin Play” script to your character. Followed by the “GridPosition” “Navigator” scripts included with Dynamic Navigation.

Thats it! Your character is now ready for Dynamic Navigation.

Tutorial 1

In this tutorial, we will go over all the basics to get you started.

Creating a grid

  1. First you must import Dynamic Navigation into your project.

  2. Now we are going to create a grid. This is simple, first create an empty game object Menu, “Game Object/Create Empty”. You should name this appropriately, for your own reference.

  3. Now add the “Grid” script to your empty game object. Menu, “Component/Dynamic Navigation/Grid”

  4. You now have a Grid editor. You should see a white square surrounding your empty game object, this represents the size of the grid you are about to create.

  1. Now we should go over a few of the basic variables before we create a grid.
    Grid Size. This is the resolution of your grid. The default is 100, this means you will have 100x100 nodes in your grid.
    Cube Size. This is the size of each square on the grid, the size you choose, should be completely dependant on the proportion of your world, and the characters in it(If you have a variety of different size characters, you can make multiple grids).

IMPORTANT: Because of the way the algorithms work, not every cube size is compatible. The way cube size operates is by half’s, quarters, eighths and sixteenths. Some examples of compatible cube sizes include, 2, 1.75, 1.5 1.25, 1.125… Lets say you wanted to make a cube size as 1.8, well that is not compatible, but the closest one compatible would be 1.875.

Step height, is the height in which the grid points will sit off the ground, this does not have a dramatic affect on results, but it can in certain instances.
Slope height, is the max height a node can be from its neighbour(Basically controls the steepness allowed in the grid).
Layers, are basically the amout of floors the grid will penetrate. So if you have a 10 story building, you will need 10 layers.
Layer height, is the minimum height before a new layer(This should be determined by the height of your characters).
Ignore Collision Layer, is the collision layer that the grid will completely ignore.
Not Walkable Collision Layer, is the collision layer that you know the AI will never walk on(This could include, boxes, rooftops).
Dynamic Obstacle Layer, is the collision layer for Dynamic obstacles(Obstacles that may move) that have the dynamic obstacle script attached to them.

All objects in your scene should be assigned to the appropriate layers. Normal static objects, that can be both walk-able and an obstacle, should not be assigned to any of these. (But you can very well get away without assign objects to any of the layers)

  1. Now that you have determined all of your variables. You can choose to “Create Grid Now”.

If you want to view your grid you can check “Debug Show Connections” as true. To see not walkable grid check “Debug Show Nonwalkable”.

Tutorial 2

Quick AI and Navigator Setup

This section we will discuss how to setup a Navigator/AI to navigate the grid you have setup. There is a pre setup AI character in the prefabs folder if you wish to use that one. Although setting up your own simple AI character for pathfinding takes little time.

  1. There are 2 scripts that need to be attached to your AI character in order for the pathfinding to work correctly. These are, “Navigator”, and “GridPosition”. The Navigator script is in charge of the pathfinding, while the “GridPostion” script is respondisble for locating where the AI is located on the grid with algorithms.
    “GridPosition” is required to be attached to to AI characters, as well as objects that are being target in pathfinding, for the pathfinding to work on both ends.

  1. Now that we have done this, lets attach the simple “AIMove” script that comes with the package(Just so we can get some movement on the path). Feel free to use an animated character, this script supports running and idle animations. To use this make sure to assign the “Character to Animate” as well assign the run and idle animations.

  2. Alright, now our character is ready to pathfind and move around. But, lets take a look at some of the variables in the inspector for “Navigator”.

Grid - This is by far the most important one. You need to assign the grid before anything will work. This is done manually cause of flexibility for switching between multiple grids.
Debugs Active - This will give you debug messages as well as give you visibility of the A* in action.
Target - The target the Navigator is currently targeting. This is generally only used when programming your own AI, where you would assign your AI’s current target to this.
Change Node Dist - The distance before the AI will change to the next node on his path. This is dependant on the size of your characters and proportions. Note: The larger the node distance, the more ‘relaxed’ their pathfinding will look, but too high will give you undesired results.
Slow To Stop - This is only used when programming your own AI. This is the Navigators way of telling the AI that a path has not yet been made, or there is no path to be found(This was made visible in the inspector to make programming visually easier). When programming your AI, it should be idle when Slow To Stop is true.
Path Speed - This is the speed that A* search will travel through the nodes. This affects performance the most out of everything. It is important to note, that even a speed of 1 is still usable to many situations Example, if the Path speed was set to 5, that would mean the search travels through 5 nodes per frame.
Static Path Speed - This is the speed the A* search travels when searching a static target. This can be set significantly higher because generally the path only needs to be calculated once. Targets are set to static through the “Grid Position” script attached to the target.(“StaticTarget”)
Max Dist - This is the maximum distance the A* will search will go for. It is important to keep this reasonable, cause if you have a very massive world and your AI cannot find a route to his target, it will continue searching until the maximum is reached or all corners have been searched.
Smooth Path - When enabled, paths are smoothed. You should always have this on for the best results.
Show Path - Shows the path of the navigator.
Navigate Target-This needs to be enabled when your AI is pursuing a target. The only times this should b disabled is when your AI is not pursuing any targets or is in idle.
Retreat Target - This should be enabled when you want your AI to retreat from his target. Note: This feature is not yet perfected, you may not get the desired results.
Retreat Distance - The distance you want the AI to retreat.

  1. Alright, now that we have got a basic understand of what the variables do, lets put this to the test! Create a cube, or an empty gameobject and add the “Grid Position” script to it. This is going to be our ‘target’. Remember to assign the grid in its inspector.

It is important to note, in the Grid Position script, the axis height, is the height at which the axis hovers off the ground. This should be set as accurately as possible, in order for the Grid Position script to work as accurately as possible.

  1. Place the target in an area you know the AI can find a route to it.

  2. Now, in the AIMove script, assign the target you made to “Target” in the inspector, and enable “Goto Target”. Press play and he should pathfind to the target if everything is set up correctly. :slight_smile:

Tutorial 3

Dynamic Obstacles Navigation Portals

In this section, we will talk about the two very useful features, Dynamic Obstacles, and Navigation Portals.

Dynamic Obstacles are objects that will move in your scene. This should never be used for characters because you will likely get undesired results. Dynamic Obstacles should only be used for objects in your scene that either spawn or move. Some examples could be, doors, objects with moveable physics like crates, barrels, even large pieces of debri from an explosion or building collapse.

Navigation Portals is a feature that gives you the ability to connect any 2 nodes on the grid, and have the ability to have either ends open or closed. This is a very useful feature that many pathfinding solutions lack. Some uses for this may include, doors locked or unlocked, ladders, elevators, jumpable areas, areas too tight for the chosen grid resolution, the possibilities are endless. A word of caution when using this feature though, placing these portals in the wrong areas can result in faulty pathfinding. If you were to place 2 portals in an open area bridging the gap of 2 nodes that are already closely connected, this may cause some strange path patterns or ‘dead ends’. You should generally only use portal navigation for nodes that would not otherwise be connected, that is what they are there for anyway.

Setting up a Dynamic Obstacle

Setting up a Dynamic Obstacle is very simple. First your object that you have chosen to be dynamic, must have collision setup properly. But dont worry, even mesh colliders work perfectly, without rigid bodies! Once your object is collision ready, simply add the “Dynamic Obstacle” script. The only thing left now, is to choose the grid. Thats it!!

Note: For dynamic obstacles to work properly, it is required that your axis is located within collision geometry(Preferably the center). The reason for this is, when the Dynamic Obstacle is creating a footprint, it builds from the inside and out like a shock wave. If your axis is located outside of collision geometry, it will start building, and not detect any collision and wont work.

Setting Up Navigation Portals

Fastest way to get started, is by going into the Dynamic Navigation Folder, then the prefabs folder, then drag in a the “Portal” prefab. Next you need to assign a grid.

The purple line represents the current node its closest to.

Now copy and paste the portal you just made. You may now assign the “Portal Connection”. Portal connection is the other portal in which it is connected to, so in this case its the one you just copied off of. The portals must both be assigned to eachother.

You will see a blue line connecting the 2 portals now.

Now that they are connected, you need to decide which sides are open(Portal Open). If both sides are checked as “Portal Open”, the AI will consider this a valid route no matter where he is. This is great for a case where you want the AI to say, go up and down a ladder, but, if you wanted to make an area where he could jump off, but not try to jump back up, you would then only make the top one “Open Portal”.

Tutorial 4

Integrating your own AI

In this section, we will talk about how integrate your own AI with this pathfinding system.

Integrating your own AI to use the pathfinding is really only a few lines of code. There are a few things that need to be both inputted to the Navigator then outputted back to your AI.

Lets say, your AI has spotted an enemy. Now we want him to use the Navigator to find a path to his enemy. Heres an example script.

//WE GET THE COMPONENT OF THE NAVIGATOR
Navigator nav=(Navigator)GetComponent("Navigator");

//LETS PRETEND 'ENEMYVISIBLE' IS A BOOL FOR WHEN THE AI HAS SPOTTED THE ENEMY, AND 'ENEMY' REPRESENTS' THE TRANSFORM OF THE ENEMY CHARACTER.

if(enemyvisible){

//LETS FIRST ASK IF THE NAVIGATORS TARGET IS ALREADY THE ENEMY, SO WE DONT ASSIGN IT EVERY FRAME
if(nav.Target==enemy){} else nav.Target==enemy;

//NOW WE MUST TELL THE NAVIGATOR TO NAVIGATE THE ASSIGNED TARGET

if(nav.NavigateTarget){} else nav.NavigateTarget=true;

//SO NOW OUR NAVIGTOR WILL START CREATING A PATH TO THE ENEMY TARGET.. BUT WE NEED TO MAKE IT SO THE AI WILL ACTUALLY FOLLOW THIS PATH ONCE IT HAS BEEN CALCULATED 
//WHERE, "CURRENTTARGETWAYPOINT" IS THE CURRENT TARGET NODE IN THE PATH OF THE NAVIGATOR, AND "CURRENTTARGET" IS GOING TO BE OUR MADE UP TRANSFORM FOR THIS SAMPLE, REPRESENTING THE CURRENT TARGET THE AI IS POINTING TOWARDS, WHICH WILL BE THE DIFFERENT NODES ON THE PATH.

CurrentTarget=nav.CurrentTargetWaypoint;

//SO FIRST, WE WILL ASK IF "SLOWTOSTOP" IS TRUE BEFORE WE TELL THE AI TO RUN ALONG HIS PATH.  THE REASON FOR THIS IS, WHEN THE NAVIGATORS "SLOWTOSTOP' IS SET TO TRUE, THAT MEANS IT HAS NOT YET FOUND A PATH, OR THERE IS NO PATH.
//WHERE IDLE IS THE STANDING IDLE STATE OF THE AI, AND RUN IS CHARGING/RUNNING ALONG PATH

if(nav.SlowToStop){
idle=true;
run=false;
}
else{
idle=false;
run=true;

}
}

Thats it! Those are the basics of using your own AI with Dynamic Navigation. The most important things to remember are, assigning the correct current target of the AI, telling the Navigator to “NavigateTarget” and telling the AI to turn towards the Navigators “CurrentTargetWaypoint”. Remember, when “SlowToStop” is true in the Navigator, your AI should not be attempting to move along the path.

Page Under Construction…

I’m a little confused! I’ve just bought Fantasy AI 2.0 and I’ve been trying, unsuccessfully, to integrate a FPS player into one of the empty AI prefabs so I can get the characters I’ve got to attack the player.

Can I do that with Fantasy AI 2.0 or do I need a different Unity asset?

I’m trying to create a zombie/skeleton/etc shootem’ up game rather than a ‘fantasy’ sword play type game.

This isnt a Fantasy AI forum… But all you need for the AI to attack any object, is a health script, team script, and the correct collision layer(Layer 9). Also keep an eye on where the axis is positioned on your player, if its on the ground its likely their raycasting vision could be obstructed. You should visit the Fantasy AI help page…

http://forum.unity3d.com/threads/129422-Advanced-Fantasy-AI-Tutorials

Thanks for the quick reply.

I’m using the Ultimate FPS Camera asset for now which isn’t quite compatible with Fantasy AI so I’ve had to add a ‘Damage’ method to your health script and change the way the projectiles work but I’ve got everything working now :slight_smile:

Cheers

Awesome. Just so you know, Dynamic Navigation is now released! Its compatible with Fantasy AI out of the box.

Asset Store: http://u3d.as/content/brilliant-game-studios/dynamic-navigation-beta-/3FZ

I have just bought this, but problem is, i’m using your default map with my fps player and a enemy model, the enemy comes towards me, but if i go round a corner he start glitching out and running backwards and forwards. Why?

Did you add a ‘gridposition’ script to your player/target? Also, when you have added the ‘grid position’ script to your player or target. You have to make sure the ‘axis height’(pivot point) is correct. So if your ‘axis height’(pivot point) on your player or target is right at his feet, you would want the axis height to be set to 0, because it is 0 meters above the ground. Hope this makes sense.

Edit: If you want to measure your axis height. Simply place your players feet at ground level, then take note of your y position. Then select your character and move him down on the y axis until the pivot point is level with the ground. Subtract your previously noted y position with the current one and that is how high your ‘axis height’ is. :slight_smile:

I think it has something to do with the grid, when i go in debug, i can see that if i stand in a non walkable area, the enemy just glitches and runs around in a circle (obviously there is no path), but the thing is, i built all the walls, then generate the grid and i get a red non walkable area on the floor, i can just stand in this and they dont come after me

:confused:

If you are getting red you likely have a collider somewhere… Also did you read about cube sizes? Not every cube size is compatible because of how the algorithms work. Also your capsule size should be around the same size as your cube size.(Generally a little smaller)

I added some pics to the post :confused:

Looks fine to me… Your cube size is waaay too large though. Make your cube size smaller. If you need to go smaller than 1, use 0.75 or 0.5 or 0.25. Make sure to match all the other numbers with it.

I will quickly explain why your characters run in circles. The grid position does not allow you to connect with a nonwalkable area… If you enter a nonwalkable area, the AI will go to the last walkable area your player/target was. They walk in circles because they have reached the goal waypoint, but are not close enough to the target to stop. This will be fixed by making a more detailed grid by making the cube size smaller.

Each square on the grid should be around the width of the AI characters themselves. This makes sense because this will allow them to enter more areas they can logically fit in, like a doorway or slim hallway.

I am proper struggling with this, maybe i don’t fully understand how these values work, etc.

Is there a way for you to have a look and let me know? Just so i get a understanding of this?

Just make your cube size smaller. Match the capsule size to the cube size… Mess around with all the variables to get a feel for it. I gave definitions for every Variable in there.

Ok, i think i got it now, i will let you know how i get on :smile:

This is my AI Move script, based on the one you provided, how would i get the enemy to look at the player at all times? They sometimes hit me whilst facing the wrong way :frowning:

http://paste.thedigitalboard.com/index.php/view/89109431

Are they zombies?

Just make him turn towards his “Target” when he is attacking…

IEnumerator DoAttack(){
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target - transform.position), 7 * Time.deltaTime);
}

Yh there zombies :smile: and thanks

That’s what I was waiting for!!! Very nice tutorial, Robert. Thanks so much:smile::smile::smile:

I’ve got a large map. I’d like to have either a grid that covers the whole map (i think too slow) or multiple grids that make up the map. If I use multiple grids, how do I make the AI use the extra grids? I’ve tried making them children of the original grid but the AI gets stuck at the border of the grid which is unwalkable by default.

2 Weeks Later, no reply. Thanks.

I have a new issue ontop of the existing issue. New project, new scene, setup the same. Nothing moves… All the navigation settings are the same. The only difference is that the character is walking on a plane instead of a terrain object. The grid is walkable but the path shows up red instead of green. I’ll keep trying to work it out but it’d be nice with some debugging that actually did something for example. Why does my AI not move? Trying to move…position not changing…please check this or that. I click ‘debugs active’ and the only debugging it gives me is ‘grid created’ which is the least helpful of any debugging. Not trying to be a dick, I paid money for this software because it looked like it worked smoothly but really, this software has a small margin for error and it does not tell you where the error is.

–Tried it with a terrain object still nothing moves. I get the error:

Path Failed
UnityEngine.Debug:Log(Object)
Navigator:Update()

Edit: Resolved the ‘objects not moving’ error. The position of the target was too far from the nav mesh however in the past this was never an issue. Making a new empty game object as a target at the same height seemed to work.