Unity Animal Path Finding Tutorial
0.0 (0 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
23 students enrolled
Wishlisted Wishlist

Please confirm that you want to add Unity Animal Path Finding Tutorial to your Wishlist.

Add to Wishlist

Unity Animal Path Finding Tutorial

A Beginner guide on how to create path finding for all the animal inside the game
0.0 (0 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
23 students enrolled
Created by Phu Tze Pang
Last updated 12/2016
Curiosity Sale
Current price: $10 Original price: $25 Discount: 60% off
30-Day Money-Back Guarantee
  • 3 hours on-demand video
  • 2 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
What Will I Learn?
  • Student will be able to create path finding script for their character in the game
  • Student will be able to understand A* Path finding concept
  • Student will be able to understand the concept of Node, Grid, Heap, IEnumerator, IEnumerable, StartCouroutine
View Curriculum
  • Student just need to install free Unity3D on their computer

What the course offers:

My course will teach you how to create path finding for all character inside the game, I will first let you download all 6 scripts and also a fox object so you can immediately play the game inside Unity. Then I will slowly explain the script one by one, concept by concept, function by function, line by line until you understand everything and make modification based on your need.  

I designed this course to be easily understood by absolute beginners. You just need to follow step by step to complete your games. You do not need to buy anything else, every tool and software you needed can be downloaded free. 

If you want to create a path finding games quickly, please give it a try and you wont be disappointed.

Who is the target audience?
  • Student who want to learn how to create path finding for their character should take this course
  • Student who want to learn C# script should take this course
  • Student who want to learn concept of Heap, IEnumerator, IEnumerable, StartCouroutine should take this course
Students Who Viewed This Course Also Viewed
Curriculum For This Course
25 Lectures
Creating Path Finding Games From Scratch
5 Lectures 28:06

1. Open Unity3D

2. Create a new project

3. Download 2 zip file and extract them into your project folder

- 6 script file into your script folder

- fox_pivot3.fbx into your animal folder

4. Create a plane and make it green

5. Create obstacle and make them red

6. Add new layer "unwalkable" and assign them to the obstacle

Creating New Project and Download 2 zip file

1. Create empty object name it A*

2. Attach 3 script into the A* object (Grid.cs, Pathfinding.cs, PathRequestManager.cs)

3. Tick the Display Grid Gizmo on Grid(script), set the world size x = 30, y = 30, radius = 0.5

4. Set unwalkable mask to your new layer "unwalkable"


If you want higher resolution for your grid, you can set the radius smaller

Example, if you set radius = 1, it will be total 15 box on X and 15 box on y

if you set radius = 0.5, it will be total 30 box on X and 30 box on y

Creating A* object and attach 3 script into it

1. Create a new cube position set at 0,0,0, scale it to 3,3,3

2. Drag the fox under the cube, adjust the fox position so that it is in the middle of the cube

3. Untick the cube mesh renderer so the cube become transparent

4. Create a new animator controller name it fox controller

5. Double click open the fox controller, drag the "Armature|run" from the fox into the fox controller

6. Drag your fox controller into your fox(on the scene) -> animator -> controller

7. Click run and make sure your fox run on the screen

8. Create a new Capsule call target

9. Drag unit.cs script into your fox on the scene

10. Drag target into fox on the scene -> unit.cs(script) -> target

11. Run the game and the fox will find a path to the target, to see the path make sure you click "scene tab"

12. You can drag your target around on the "scene tab" and the fox will change path to your target new position

13. To avoid the fox from overlapping each other, you need to add rigid body to the fox

14. Add rigidbody to the fox, with constraint freeze position y, freeze rotation x, z

Putting fox into the game

1. Scale the fox under the cube to 0.5,0.5,0.5

2. Scale the fox cube to 1,1,1

3. You can always off the Display grid gizmo so that it will not show the grid

Preview 02:17
Explaining Grid.cs and Node.cs Script
3 Lectures 25:48

1. Grid let you specify the world size and set how big is each node and how many node inside the world, in my tutorial I specify my grid size to be 30 x 30 where each node diameter is 1(radius is 0.5)

2. Each node have 9 variable

3. gcost is how far the current node from the target

4. hcost is how far the current node from the starting point

5. fcost is gcost + hcost

6. parent is from which node you come from

Explaining Grid.cs and Node.cs

1. Node diameter = Node radius * 2

2. On the function CreateGrid(), it will loop every box on the grid and instantiate a new node

3. You calculate the worldbottomleft base on your A* location 

4. Then you calculate the node world position base on the worldbottomleft

5. When doing the calculation, computer will always do the multiply and division first, before doing the adding and subtract

Explaining Grid2

1. On GetNeighbours() function, it will loop 9 times to scan for all 8 neighbour node and if they are inside the Gridsize it will include them into neighbours list

Explaining Grid3
Explaining Heap.cs Script
3 Lectures 22:28




Let say you have 1000 node, 

if 1 node take 1 milliseconds, 

normal for loop:

you need 1000 milliseconds to get the smallest value because you need to loop 1000 times


you only need 1 millisecond to get the smallest/biggest value


Because you already sorted them when you add new node into the tree structure of heap

PURPOSE OF IComparable<T>


Preview 04:25

Everytime you add a new item into Heap, it will add it into the bottom of the heap tree, so you need to use the "Sort up" function to sort it up, comparing it with its parent, if it is smaller than its parent, it will swap the position with its parent

Explaining Heap add function and sort up function

Heap Sort Down Function

When you remove the first item from Heap, you will need to find the next smallest number to be the first item for the heap, you do this by using the "sort down" function

Sort down function will compare the left child vs right child, and choose the smallest one to be the first item, if for example after it choose the left child to be the first item, it need to go to its left child and sort down again until it reach the bottom of the tree

How do other script use the Heap ?

Heap<Node> openset = new Heap<Node> (grid.Maxsize);    //instantiate a new heap

openset.add(Node1);    //add a node into the heap

openset.RemoveFirst();   //get the first item from the heap, which is the smallest number among all

Explaining Heap Sort Down Function and How Heap was Used by other script
Explaining Unit.cs Script
2 Lectures 18:41

1. You need to attach unit.cs script to the fox or any character that want to find path and follow path

2. You can set the target by dragging the target into the target column 

3. You can also set the running speed and rotation speed for your fox or character

4. Vector3 [] path will store all the waypoint for the fox to follow, the fox will follow from waypoint1 until the target

5. On the update function, the fox will check if the target change its location every seconds and update its waypoint accordingly

Preview 06:46

1. The purpose of using StartCoroutine is so that all 3 fox will not reach the target in one seconds but you move fox1 for one step, then you move fox2 for one step then you move fox3 for one step

2. IEnumerator and yield return null is used together with StartCoroutine, you need them both for it to work

3. If you remove the yield return null you will see that all fox reach the target in 1 seconds

4. OnDrawGizmo function will draw the waypoint on the map for you to see

Explaining Unit Script 2
Explaining PathRequestManager.cs script
3 Lectures 13:56

Purpose of using Path Request Manager

You want to queue the path request

Purpose of using Queue

You want the list to be first in first out

Purpose of using struct

It helps you to make a single variable hold related data of various data types.


Use STRUCT if instances of the type are small, its cheaper than CLASS on memory usage

Explaining PathRequestManager Script 1

1. On PathRequest() function, you enqueue a new path request into the path request manager queue

2. On TryProcessNext() function, you provide path start and path end to the pathfinding class, you get the waypoint back

3. FinishProcessingPath() function will be called by pathfinding class once the waypoint have been found, then it will call the TryProcessNext() function

Explaining PathRequestManager Script 2

Explaining PathRequestManager Script 3
Explaining Pathfinding.cs script
9 Lectures 01:03:26

Purpose of PathFinding

Give input parameter (startpath, endpath), return output Vector3 [] Path

Explaining Pathfinding.cs script1

Purpose of Heap

Return the lowest fcost in 1 miliseconds

Purpose of Hashset

An unordered collection for you to quickly check if item is inside the set






Explaining Pathfinding.cs script2

1. Add start node into the open set

2. Get the smallest fcost node from the open set and assign as current node, remove currentnode from open set and add into close set

3. Check if current node reach target, if not, get all neighbour of current node

4. For each neighbour, calculate the gcost, hcost and fcost

5. Add all neighbour to the openset

6. repeat from step 2 until target reach


gcost = cost from start, hcost = cost to target

Horizontal distance = 10, Diagonal distance = 14

Explaining Pathfinding.cs script3

1. Here I will simulate how the program find the nearest path by writing down the gcost, hcost and fcost for each node the program examine

2. Node that have been examined will be put into closed set

3. Neighbour of current node that have not been examined and not in closed set will be put into open set

Explaining Pathfinding.cs script4

Here I show you how the program navigating from node to node, setting the parent node = the node it came from

Explaining Pathfinding.cs script5

Here I continue simulating how the program work

Explaining Pathfinding.cs script6

Finally when the current node = target node, it will jump out from the while loop and call the RetracePath() function

Explaining Pathfinding.cs script7

RetracePath() function will retrace the path from the target all the way back to the starting point and store the waypoint inside the List<Node>path then return it all the way to the Unit.cs

Explaining Pathfinding.cs script8

SimplifyPath() function will remove the waypoint if the waypoint is in the same direction in order to reduce the number of waypoint

Explaining Pathfinding.cs script9
About the Instructor
Phu Tze Pang
4.3 Average rating
36 Reviews
354 Students
5 Courses

Hi, My name is Freddy Phu Tze Pang and I work as a programmer for more than 10 years. During my free time, I like to create computer games for fun. I used to use java as my programming tool to code for android but ever since Unity been launched, I have been using Unity to create games.