Lab 09 - AI



Basic challenge

The most fundamental and common problem in many games is pathfinding. The basic challenge is about learning how to use provided pathfinding code.

Pathfinding is one of the most fundamental elements of game AI, and it's quite useful in many types of games. Unity doesn't have built in libraries for pathfinding and so developers either have to implement the algorithm from scratch, or use a package from the Asset Store. The implementation provided in this lab is derived from Simply A*, a free pathfinding package from the Unity Asset Store. However, whereas Simply A* was designed for 3D pathfinding, with some (somewhat buggy) support for 2D, the code in this lab has been rewritten and simplified specifically for 2D pathfinding. The objective of this lab is not necessarily to explain the details of that implementation, but rather to demonstrate how to use the provided code in you game, so that you can use it for your pathfinding needs.

The grid

  1. Fork the Pathfinding project on GitLab, then clone your fork to your hard-drive.
  2. Open the Pathfinding project, the directory of the cloned repository, in Unity.
  3. Set your project workspace so that you can see both the "Scene" and "Game" windows at the same time.
  4. Save the scene as "Level1".
  5. From Assets/Sprites, drag the "desert" sprite into the Hierarchy panel - should create game object of the same name. Set the Transform component of the "desert" game object as shown in the screenshot below.
  6. Create an empty game object, name it "grid" and add the "AStarGrid" script as its component (the script is already there in your assets). The pathfinding algorithm in this lab operates on a regular grid, and "AStarGrid" creates this grid. You can specify the starting and end point of the grid, as well as the tile size. To make the span the "desert" background image, set the attributes of the "A Star Grid" component as follows: TileSize = 1, MapStartPosition = (-5.5, -5.2), MapEndPosition(6.5, 6.5).
  7. Enable the "Draw Grid In Editor" flag of the "A Star Grid" component and press play. The grid should be drawn in your "Scene" window, as shown in the screenshot below:
  8. The nodes are waypoints that pathfinder uses to make the path. The lines between waypoints give possible connections. At the moment all nodes are connected with their neighbouring nodes.

    Details of the implementation of pathfinding are not going to be explained in this lab. However, you can take a look at the code, and read through the comments if you wish to understand it and/or modify it.

    The tiles of the grid are squares and the grid is created so that there is an integer number of tiles. As a result, the grid doesn't cover the background perfectly. If you're using this pathfinding script in your game later on, you'll probably need to spend a bit more time, making sure the grid covers the background right. For this lab however, this doesn't need to be perfect, so just leave it as it is.


Once you have the pahtfiniding grid setup, here's how to get a game object to move on it.

  1. Drag the "packman" and "ghost" sprites from Assets | Sprites to the Hierarchy panel - corresponding name sprite game object should get created. Set the "pacman" Transform attribute to: Position = (-4, -4, -1), Scale = (2.5, 2.5, 1), and "ghost" Transform to: Position = (3.5, 2.5, -2), Scale = (2.5, 2.5, 1).
  2. Add the "AStarPathfinder" script component to the "ghost" game object. This script is responsible for finding the path and moving an object along it at specified speed. The movement is done by setting of the position of the object's transform - it should be relatively easy to tweak the script to do movement using forces, if that's what's required for your game.
  3. The "AStarPathfinder" works on a grid, and so you need to associate it with a grid game object. Drag the "grid" game object to "AStarPathfinder"s Grid Object attribute.
  4. The "AStarPathfinder" script updates the path at certain frequency. Pathfinding takes some computer resources, and it can often be updated at an interval that is slower than the frame rate. The Update Frequency attribute of the AStarPathfinder controls this frequency. It specifies number of updates per second. The default is 5 - leave this number as it is.
  5. Add the "Chase" script as a component of the "ghost" game object. This is a very simple script, which on each update call "AStarPathfinder"'s goTowards method to moves towards a target at specified speed. Make the "pacman" game object the target by dragging it to "Chase" script component's target attribute. Set Speed to 4.
  6. Press play. The ghost should travel towards the "pacman" game object.
  7. The "ghost" ignores the walls of the background image, because the grid is not aware of those walls. From the main menu select Edit | Project Settings | Tags and Layers and create a new tag name "Wall".
  8. Select the "grid" object, and specify "Wall" as one of the disallowed tags: expand the Disallowed Tags attribute of the "A Star Grid" component, set its Size to 1 and set Element 0 to "Wall".
  9. Under the "desert" game object create an empty game object, name it "wall1" and set its Tag to "Wall".
  10. Add a BoxCollider2D component to the "wall1" game object. Click the "Edit Collider" button and drag the collider to cover the wall of the "desert" sprite as in the image below:
  11. Duplicate "wall1", name the new object "wall2" and adjust its collider to cover a different wall. In all create a total of 7 walls, with colliders configured like in the figures below:
  12. If you press play now, the ghost should avoid the walls.
  13. If you take a look at the grid in the "Scene" view, you'll notice that the grid doesn't go over the walls. The "AStarGrid" script will not create a grid over colliders that belong to objects with one of the "Disallowed Tags". In this case, the disallowed tag is "Wall", which is assigned to all 7 walls.

  14. Did you notice that the grid is a bit too coarse? There ghost is not taking an obvious path between two walls (when it's close to the pacman), because the grid doesn't have a path there. Select the "grid" object and set the Tile Size attribute of its "A Star Grid" component to 0.5. Play the game again. The grid should be finer, with more waypoints, and the ghost should take the path between the walls.
  15. One thing to be aware of, is that the finer the grid, the more time taken for pathfinding - because paths are longer (in terms of the number of nodes), and there are more options to consider.


Whereas the ghost chases a specific target, the pacman is going to be mad to find best path to point marked by a mouse click.

  1. Select the "pacman" game object and add "AStarPathfinder" game object to it. Drag the "grid" game object over the Grid Object attribute of the A Star Pathfinder component. Set the Update Frequency to 0 - this means the path will be updated as soon as required. This could potentially mean an update every frame, but "AStarPathfinder" updates only its path whenever target destination changes. So, new path won't be necessarily computed per every frame.
  2. Add the "Follow" script as a component to the "pacman" game object. This script is a simple script that uses "AStarPathfinder" to find the best path to position denoted by a mouse click, and moves the game object at specified speed. Set the "Speed" of the Follow component to 8.
  3. Run the game. Click on various locations in the "Game" view - pacman should find a way to get there. If you click somewhere where pacman cannot go (outside the grid or a wall) pacman will not travel there.

That's all for the basic challenge. You can use the code from this lab in you game for pathfinding. The pathfinding algorithm uses regular grid with a Euclidean distance as the heuristic (the guess). The game objects that use pathfinding do not need to be position exactly at the node, but they must be on the walkable part of the grid. The "Chase" and "Follow" scripts demonstrate how to call the pathfinding library to get objects moving. You might need to read through "AStarPathfinder" and/or "AStarGrid" code at some point (it's all commented) and perhaps tweak it bit, to make it work for your game.


Show your work to the demonstrator for assessment.

Intermediate Challenge

Learn how to use Unity's built in system for Navigation and Pathfinding. It's a little bit more complicated than the system shown in the Basic Challenge, but it's also more powerful - it can works in 3D (and can be made to function over 2D). Create a new scene in the project form the Basic Challenge and redo the pathfinding using Unity's system.


Show your work to the demonstrator for assessment.

Master Challenge


Show your work to the demonstrator for assessment.