This is the Get Started Guide for the A* Project.
To begin with, download the project.
Next, open the example scene “Terrain” in Unity.
Click on the “A*” gameObject in the hierarchy view, in the inspector you will now see a bunch of controls, click on the Show Grid button (almost at the top) to make it enabled if it isn’t already, in the scene view you can now see a grid covering the terrain.
This grid is what the units can navigate around on, each line represents one connection and each dot from where the lines are drawn are nodes, the units will navigate from node to node to reach the target.
You will notice there are holes in the grid, that’s where there are obstacles or the slope is to step for the units to climb, those nodes are marked as unwalkable.
You will also notice the big white box around the grid, that box is the boundary of our grid, outside that grid, no path’s can begin or end.
The grid’s nodes have different colors, all nodes in one area are sharing the same color, the areas are separated by unwalkable nodes, so units can’t travel from one area to another one, most often you would want your world to have only one area (and thus only one color).
Back to the controls.
You know that many controls in the editor have tooltips, so if you are unsure about the meaning of a variable, hover over it with the mouse and see if I have put a tooltip there.
The first row of controls in the editor is a toolbar with names like “Static Settings”, “Runtime Settings” etc.
First I will talk about the Static Settings.
Static Settings
Here are the settings which should only be changed in the editor when not playing, if these are changed during runtime errors might occur.
The most important control in the static settings is the button called “Scan Map”, it is placed almost at the bottom, if you click the button the grid will update with your chosen settings (you might have to rotate the camera in the scene view to see the grid updates), it will also be called on startup if you have the “Calculate Grid On Startup” toggle checked.
Grids
The higher up in the list a grid is the higher priority it will have.
For example if you make a large grid for a dessert you might want to use large nodes, but in that dessert there is a small house with more detailed geometry, then you might want to create a second grid with smaller nodes for just that house
but how shall the script know which grid it shall use when both grids contains the house, it will select the one highest up in the list, so make sure your house grid is higher up than the world grid.
For more info about multiple grids, see the “Links” area.
Here is an overview of what the grid variables do, notice that some of the physics variables only show up when using certain modes and are not always visible.
Variables
|
Show Debug |
Will the scene view show debug info about grid or not |
|
Width |
The number of nodes in the grid along the X axis |
|
Depth |
The number of nodes in the grid along the Z axis |
|
Node Size |
The size of each node, multiply this with Width or Depth to get the world width/depth of the grid |
|
Height |
The height variable requires a bit of thought, make sure the white box contains the full area you are going to start a path from/target a path to, remember most units wont calculate a path from their feet but from their pivot point which is usually a bit higher up. This variable uses world units and not [node size] units like the Width and Depth variables |
|
Offset |
This is the location of your grid in world space, change the values to move your grid around |
|
Physics |
Here goes all variables for how the grid detects obstacles |
|
Walkable check |
These are the options you have for detecting obstacles. |
|
Height |
When using Capsule mode this defines the height of the capsule |
|
Radius |
When using Capsule, Touch or Overlap mode this defines the radius of the capsule/sphere to check inside, 1 is equal to one node, not one world unit |
|
Mask |
This defines all layers which will be identified as obstacles, for example you would not want the ground layer to be enabled since that’s what the character will walk on. |
|
Direction |
The direction of the raycast, not much to say |
|
Length |
The length of the ray to cast in raycast mode |
So these were the Grids, but what if you don’t want a flat grid, but instead would want the grid to follow your terrain, here’s the settings for that.
There are three different settings for defining the position of the nodes along the Y axis, the first is Flat, that generates a flat grid, no settings for it, the second one is the Terrain mode, this will sample the terrain height at each node position and place it at the correct height.
A warning for terrain mode though: If you use a large terrain (I think that’s the reason, it’s on the Unity side), then you might get the “vertexCount > 60000″ error, that means the script can’t sample the height values, to get rid of it you will have to change to raycast mode, place the terrain in a separate layer and assign the raycast to only hit that layer, then it will work just the same.
The last mode is called raycast mode, it uses raycasting to figure out where to place the nodes, it does also have more features than the terrain mode since it can get the normal of the point it hit too, this makes it possible to make nodes with a too high slope unwalkable, the variable for changing that is called Static Max Angle, it can also be used with terrain mode but with the drawback that the terrain mode can only use the angles between the nodes and not the real normal which makes it a bit unreliable.
I suggest that you use raycast mode in all cases if your grid shouldn’t be completely flat.
Variables
|
Use Normal For Max Angle Calculation |
This toggles the use of normals in raycast mode, otherwise it will only use the angles between the nodes |
|
Static Max Angle |
The max slope at which a node can be placed at and still be considered as walkable |
|
Mask |
This defines all the layers the raycast will hit, this should be the layers the player/NPC/whatever should be able to stand on and not things like walls and things the pathfinding should avoid |
Next up comes the Neighbours variable.
If you look closely at the grid you should see that each point has eight or four lines (depending on which option is chosen) drawn from it to neighbouring nodes, in some games you might want the player/NPC/whatever to move diagonally, and in some games you might not want that, so that’s why I have given you the option to choose what you like the best, try it out and change the setting and see what happens.
The last variable is the heap size variable. This one is quite advanced and it’s tricky to know what it does but I will try to explain.
If you have a basic understanding of A* you should know that the script stores an “open” list of all possible nodes it could move to next time it iterates (one iteration for each node it searches), the next one it chooses is the node with the lowest F value, this is the distance travelled from the start added to the estimated distance to the end. So the script needs to search for the node with lowest F value in an array maybe 200, maybe 2000 times in one frame, that isn’t so fast if we are going to search through the whole list every time, so the script uses a so called Binary Heap (look up on wikipedia for more info) to make it faster.
What the heap size variable adjusts is the maximum length of the binary heap array, 1 means that the whole grid can be in the open list if required (this will never happen) but it uses more memory (if a grid is 100*100 nodes it will create an array of length 10 000), 0.1 means 10% of the grid can be in the open list and it uses a lot less memory. I recommend that you use the value 0.5, that was the default value in all pre 2.5 versions by the way. Maybe you should use higher values for small grids.
If you haven’t understood a thing of what I said about the binary heap… Don’t worry, you will likely not have to care about it anyway ; )
So now you have had a complete description of the Static Settings (sorry if it was a bit long).
I the next part I will show you how to set up a grid in a new scene, Part 2
the vertexcount > 60000 error happens whenver the used grid is too large. Basically you have 170 x 170 at very maximum but it seems like the grid generation code does something strange because it even fires this error with 100×100 and nothing to cast against at all?
I’ve not checked the sources but potentially it creates intermediate mesh data that are not correctly cleaned up?
I should mention that it normally goes away if the scan is done multiple times
The vertexCount > 60 000 error is a Unity bug, the terrain script doesn’t seem to be able to handle GetHeight () on large terrains. Don’t know why though…
Thank you for posting a bit more info about this.
Hey Aron
I have the Terrain Scene open, and I am trying to go through the get started guide you wrote here. It might just be me, but I cannot find the A* game object in my project view at all. I can find the A* Instructions in the Hierarchy but I don’t suppose that is what I am looking for. I have been looking through all the folders in the Project but I must be a bit blinded :S
Hi Mikkel
The A* gameObject is a child under the “searchers” gameObject.
Hey Aron, Great Idea btw, But I am just a newb and need help
I keep getting an error about putting a layer on?
Im not exactly sure on how to implement the system in my game
I have downloaded and loaded the package and everything seems good
but I do not get any walkable node.
I love the way it all works in the terrain scene but have NO IDEA on how to make it work in my own
help would be appreciated thanks
Hi Dominic
1. You are probably referring to the “No nodes are walkable” error, take a look at the last rows in part two and see if that helps you.
2. In the documentation there is a page called “How do I use the A* code with my project”, take a look at that, I will update that page with some more detailed information soon though.
3. See 1.
Hey Aron,
Great work!! I really want to use this on my project!
It’s a little scetchy on how to impliment it though. I’m still struggling to figure out how you got the ray-casting to work on your house demo. It seems to generate the all red path nodes for me.
I think some video tutorials on how the samples were made would be really useful. I’d be willing to make them for you if you help me get a handle on how this thing works. I’ve written an Astar system before in another language, so I know how the guts work. I just need to understad your interface.
Also, seeing as I’m a computer artist for a living, I could trade you some game artwork for support. If that’s something your interested in.
Why does the [b]Walkable Check[/b] setting not retain the [i]Mask[/i] value?
I’ve been having the worst time with this. This system looks extremely cool and feature-packed, and I’d very much like to get it working, but I simply haven’t been able to do so. It crashes Unity constantly, and at best just doesn’t seem to work for me.
I’m a sad panda
Huh, I haven’t heard of that before.
Do you set the mask to a value, and then it get’s reset, is that what happens? (remember that selected layers are shown by an “O” to the left of the layer name, unity hasn’t made real layerMask fields possible to do in editorGUI).
And if it crashes, what do you do right before it crashes? (are you using windows or mac?).
Also make sure you have multithreading disabled.
Sorry for answering late, I have been (and am) on holiday, but I’m home for one day so I will try to answer as many comments as possible.