{"id":77860,"date":"2018-03-29T20:30:50","date_gmt":"2018-03-29T20:30:50","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=77860"},"modified":"2021-07-29T19:44:15","modified_gmt":"2021-07-29T19:44:15","slug":"pathfinding-unity-c","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/pathfinding-unity-c\/","title":{"rendered":"Pathfinding with Unity and C#"},"content":{"rendered":"<h4>The series so far:<\/h4>\n<ol>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/introduction-game-development-unity-c\/\">Introduction to Game Development with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/using-c-functions-animations\/\">Using C# Functions in Your Animations<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/procedural-generation-unity-c\/\">Procedural Generation with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/pathfinding-unity-c\/\">Pathfinding with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/extending-unity-editor-c\/\">Extending the Unity Editor with C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/using-unity-ui-and-c-to-create-a-tic-tac-toe-game\/\">Using Unity UI and C# to Create a Tic-Tac-Toe Game<\/a><\/li>\n<\/ol>\n\n<p>When creating enemy AI (Artificial Intelligence) it is often important to specify paths for your enemies. Enemy pathfinding can be useful for all types of games, be it stealth games or even fast paced action games. This pathfinding is often done by using a navigation mesh, or \u2018navmesh,\u2019 to find their way around. A navmesh is a type of data structure that is applied to objects to aid in pathfinding through complicated spaces. Video games began to see navigation meshes used more frequently as early as 2000.<\/p>\n<p>As with many things, Unity has a simple way to create a pathfinding system for your objects. This easy process will allow you to get your basic AI off the ground quickly and allow you to create a more complex AI from there. In this example you&#8217;ll be creating a navigation mesh that an object will use to make its way to several points that you place in the environment. You will craft an area that you can \u2018bake\u2019 a navigation mesh in to start, create an object, and give the object a script that it will use to travel to different waypoints. From there you will set up these waypoints and assign them to the object in order to create a path for your object to follow endlessly.<\/p>\n<h2>Setting Up<\/h2>\n<p>First you will need to create a project. Open Unity and select <em>New<\/em> in the top right corner of the window as shown in Figure 1.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"996\" height=\"131\" class=\"wp-image-77861\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-15.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 1: Creating a new project<\/p>\n<p>Name this project <em>Pathfinding<\/em> and make sure it&#8217;s a 3D project (it should be by default). Specify a file path for your project. Your project creation window should look similar to Figure 2. Click <em>Create project<\/em> once the form is complete.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"582\" class=\"wp-image-77862\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-16.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 2: Naming the project<\/p>\n<p>Allow Unity a moment to create the project. Once it\u2019s finished, the first thing that you&#8217;ll need to do is create an area for the navmesh. Start by selecting the <em>Create <\/em>menu in the <em>Hierarchy <\/em>window then selecting <em>3D Object <\/em>and then <em>Cube<\/em>. Figure 3 shows where to select the cube object for creation.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"376\" height=\"333\" class=\"wp-image-77863\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-17.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 3: Creating a new cube object<\/p>\n<p>This first cube object will be the floor of this scene. Name the object <em>Floor <\/em>and give it an <em>X Scale <\/em>of 20 and a <em>Z Scale <\/em>of 20. Figure 4 shows the final result of the <em>Floor <\/em>object in the <em>Inspector<\/em> window.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"125\" class=\"wp-image-77864\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-18.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 4: Renaming object to Floor and setting its scale values<\/p>\n<h2>Creating the Scene<\/h2>\n<p>Now you should make some obstacles for the object to navigate around. Your scene can be as simple or as complex as you like. Figure 5 shows an example scene that you can use.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1328\" height=\"519\" class=\"wp-image-77865\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-19.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 5: An example scene<\/p>\n<p>If you want a better view of your scene when creating it, you can move the scene camera around by right-clicking in the <em>Scene <\/em>window and moving the mouse. Zoom in and out of the <em>Scene <\/em>window by scrolling your mouse wheel in the <em>Scene <\/em>window until you see everything you want view.<\/p>\n<p>To create some obstacles, go to the <em>Create <\/em>menu once again and under <em>3D Object <\/em>select <em>Cube<\/em>. Duplicate the cube as many times as you wish, then set a scale, rotation, and position for each cube using the <em>Transform <\/em>component in the <em>Inspector <\/em>window. Note: the cube objects will probably need to have a <em>Y Position <\/em>value of about 0.25, this way the cubes won&#8217;t be below the floor. Also, it is recommended that the cubes should have a <em>Y Scale <\/em>of at least 2. Your cube objects can be arranged in any way you like, but if you wish to replicate the scene in Figure 5 you may refer to Table 1 for the sizes and positions.<\/p>\n<p class=\"caption\">Table 1: The position and scale of all objects used in the example figures<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>Object<\/p>\n<\/td>\n<td>\n<p>Position X<\/p>\n<\/td>\n<td>\n<p>Position Y<\/p>\n<\/td>\n<td>\n<p>Position Z<\/p>\n<\/td>\n<td>\n<p>Scale X<\/p>\n<\/td>\n<td>\n<p>Scale Y<\/p>\n<\/td>\n<td>\n<p>Scale Z<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Cube<\/p>\n<\/td>\n<td>\n<p>1.13<\/p>\n<\/td>\n<td>\n<p>0.25<\/p>\n<\/td>\n<td>\n<p>0<\/p>\n<\/td>\n<td>\n<p>10<\/p>\n<\/td>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Cube (1)<\/p>\n<\/td>\n<td>\n<p>6.04<\/p>\n<\/td>\n<td>\n<p>0.25<\/p>\n<\/td>\n<td>\n<p>5.87<\/p>\n<\/td>\n<td>\n<p>7<\/p>\n<\/td>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>3<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Cube (2)<\/p>\n<\/td>\n<td>\n<p>-3.73<\/p>\n<\/td>\n<td>\n<p>0.25<\/p>\n<\/td>\n<td>\n<p>-5.32<\/p>\n<\/td>\n<td>\n<p>9<\/p>\n<\/td>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>3<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Cube (3)<\/p>\n<\/td>\n<td>\n<p>-3.73<\/p>\n<\/td>\n<td>\n<p>0.25<\/p>\n<\/td>\n<td>\n<p>4.64<\/p>\n<\/td>\n<td>\n<p>7<\/p>\n<\/td>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>4<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>Cube (4)<\/p>\n<\/td>\n<td>\n<p>6.05<\/p>\n<\/td>\n<td>\n<p>0.25<\/p>\n<\/td>\n<td>\n<p>-5.87<\/p>\n<\/td>\n<td>\n<p>5<\/p>\n<\/td>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>5<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Once you&#8217;ve set up your cubes, go ahead and parent them to the <em>Floor <\/em>object by selecting all your cube objects and dragging them onto the <em>Floor<\/em> object in the <em>Hierarchy<\/em>. Note: In the <em>Inspector <\/em>window it will look like the positions and scales change after parenting them to the <em>Floor<\/em> object. These values are their local position and local scale. You do not need to edit them.<\/p>\n<p>If you wish, you may also create a material for the floor so that you can more easily tell the difference from the floor to the walls you have created. To create a material, right click in your <em>Assets <\/em>window and select <em>Create-&gt;Material <\/em>like in Figure 6.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"698\" class=\"wp-image-77866\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-20.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 6: Creating a new material<\/p>\n<p>After creating the material, give it the name <em>Floor <\/em>as seen in Figure 7.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"247\" height=\"64\" class=\"wp-image-77867\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-21.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 7: Renaming the material to Floor<\/p>\n<p>Now you just need to set a color other than white for it. With the <em>Floor <\/em>material selected, head over to the <em>Inspector <\/em>window and double-click the color picker near the top like in Figure 8.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"251\" class=\"wp-image-77868\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-22.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 8: Opening the color picker<\/p>\n<p>In this example, the color has been set to grey, but it can be whatever color you desire. Figure 9 shows the Unity color wheel and the RGB values used in this example.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"297\" height=\"499\" class=\"wp-image-77869\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-23.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 9: Example color for the Floor material<\/p>\n<p>Finally, to set the <em>Floor <\/em>material to the <em>Floor <\/em>object, select the <em>Floor <\/em>object from the <em>Hierarchy <\/em>and under the <em>Mesh Renderer <\/em>component select the <em>Materials <\/em>option like in Figure 10.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"571\" height=\"217\" class=\"wp-image-77870\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-24.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 10: Opening the Materials menu<\/p>\n<p>This will reveal some properties where you can change the material the object is using. Click and drag the <em>Floor <\/em>material from the <em>Assets <\/em>window into the <em>Element 0<\/em> field in the <em>Inspector <\/em>like in Figure 11. Once complete, your floor will have a grey color to it, making it easier to tell apart the floor from the walls.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"347\" class=\"wp-image-77871\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-25.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 11: Applying the material to the Floor object<\/p>\n<p>Now there needs to be an object that to navigate the area you have created. Once again, open the <em>Create <\/em>menu at the top of the <em>Hierarchy <\/em>and navigate to <em>3D Object-&gt;Cube <\/em>to create another cube object. Be sure to set its position values to <em>X 0, Y 1, Z 1.5<\/em> in the <em>Inspector <\/em>window. Note: you may need to adjust this depending on your scene layout. Give the cube the new name <em>Object<\/em>. When finished, your <em>Hierarchy <\/em>window should look similar to Figure 12.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"318\" height=\"206\" class=\"wp-image-77872\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-26.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 12: The current Hierarchy window<\/p>\n<p>You can come back to this object in a moment, but before continuing you should go ahead and create the navmesh that will be used in your project. To do this you will need to navigate to <em>Window-&gt;Navigation <\/em>to open the <em>Navigation <\/em>window. Figure 13 shows where to find this.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"341\" height=\"691\" class=\"wp-image-77873\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-27.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 13: Opening the Navigation window<\/p>\n<p>This should open the <em>Navigation <\/em>window where the <em>Inspector <\/em>window was earlier. By default, <em>Object<\/em> should be selected in the <em>Navigation <\/em>menu. Before continuing, make sure that the <em>Floor <\/em>object is selected in the <em>Hierarchy <\/em>window, else the process of creating the navmesh won&#8217;t work. Make sure <em>All <\/em>is selected under <em>Scene Filter <\/em>(once again, it should be by default) then check the <em>Navigation Static<\/em> checkbox. If you see a message asking if you want to enable the Navigation Static flag for the child objects, click <em>Yes, change children<\/em>. When finished the <em>Navigation <\/em>window should look like the one in Figure 14.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"236\" class=\"wp-image-77874\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-28.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 14: Setting properties for the navmesh<\/p>\n<p>Next, select the <em>Bake <\/em>option in the <em>Navigation <\/em>window as seen in Figure 15.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"325\" height=\"77\" class=\"wp-image-77875\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-29.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 15: Opening the Bake menu<\/p>\n<p>Once selected you will be greeted with the <em>Bake <\/em>menu. This is where the navigation mesh will be created. You can set properties like the radius and height here to change how the navmesh will turn out once it is finished \u2018baking,\u2019 which is the process of creating the navigation mesh from Unity&#8217;s end. For this project, you may leave all properties at their default values. Before creating the navmesh, you will need to save your scene. Once your scene is saved, go ahead and select the <em>Bake <\/em>option near the bottom of the <em>Bake <\/em>menu as seen in Figure 16 to create the navmesh.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"582\" height=\"427\" class=\"wp-image-77876\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-30.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 16: Baking the navmesh into the scene<\/p>\n<p>You may need to allow a moment for Unity to create the navigation mesh before continuing. After Unity completes the navmesh creation process, your scene window should look similar to Figure 17.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1328\" height=\"519\" class=\"wp-image-77877\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-31.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 17: Example navmesh<\/p>\n<p>Switch back to the <em>Inspector <\/em>window by clicking <em>Inspector<\/em> near the top of the <em>Navigation <\/em>window like in Figure 18.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"431\" height=\"250\" class=\"wp-image-77878\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-32.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 18: Switching to the Inspector window<\/p>\n<p>Select <em>Object<\/em> in the <em>Hierarchy <\/em>to complete the setup. The last thing you need to do is give <em>Object <\/em>a new component called <em>Nav Mesh Agent<\/em>. To do this, click the <em>Add Component <\/em>button in the <em>Inspector <\/em>window. Figure 19 shows where this button is.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"258\" class=\"wp-image-77879\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-33.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 19: Adding a new component<\/p>\n<p>Using the search bar, search for <em>Nav Mesh Agent<\/em>. Once found, click on the component like in Figure 20 to add it to the <em>Object <\/em>object.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-77896\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/navmesh1.jpg\" alt=\"\" width=\"617\" height=\"347\" \/><\/p>\n<p class=\"caption\">Figure 20: Adding the Nav Mesh Agent component<\/p>\n<p>The <em>Nav Mesh Agent <\/em>component allows an object to navigate the scene using the navmesh you just created. There are several properties you can change such as how fast it goes, if it automatically brakes, or adjusting the obstacle avoidance radius. In this project you can leave all those alone if you wish, as the default settings will work perfectly with this project. Now the only step remaining before adding C# code is to create another new component for the object, this time a script component. Click the <em>Add Component <\/em>button again and this time search for \u2018new script. Once found, select new script\u2019 to create a new script. Give it the name <em>Pathfinding <\/em>and make sure the language is set to <em>C Sharp<\/em>. Select <em>Create and Add <\/em>to create the script and attach it to <em>Object<\/em>. Figure 21 shows how the setup should look.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"595\" class=\"wp-image-77880\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-34.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 21: Creating the Pathfinding script<\/p>\n<p>With the new script created, it is now time to open Visual Studio and create the <em>Pathfinding <\/em>script. Double click the <em>Script <\/em>field under the <em>Pathfinding<\/em> component like in Figure 22 to open Visual Studio and begin writing this object&#8217;s pathfinding code.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"146\" class=\"wp-image-77881\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-35.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 22: Opening the script in Visual Studio<\/p>\n<h2>Creating the Code<\/h2>\n<p>Before any code can be written a library will need to be added in Visual Studio. Just underneath the line <strong>using UnityEngine;<\/strong> enter the following line of code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">using UnityEngine.AI;<\/pre>\n<p>This line will allow you to get the <em>Nav Mesh Agent <\/em>component that you attached earlier to <em>Object<\/em>. It will also give you the tools needed to have the object move between different points you&#8217;ll be setting up for it in a moment. Now, just underneath the line that says <strong>public class Pathfinding : MonoBehaviour <\/strong>enter the following code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public Transform[] points;\r\n  private NavMeshAgent nav;\r\n  private int destPoint;<\/pre>\n<p>First, you will be making a public array of Transforms simply called <strong>points<\/strong>. This array will store the locations of the different points that will be set up in the next section. The object this script is attached to will make its way to each point one by one until you end the game. It will navigate towards these points using the <em>Nav Mesh Agent <\/em>component, and the code will store a reference of that component in the <em>nav <\/em>variable. Finally, there&#8217;s the <strong>destPoint<\/strong> integer, which will be used to cycle through the different points laid out for <em>Object<\/em>. At this point, your code should look similar to the code seen in Figure 23.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"408\" class=\"wp-image-77882\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-36.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 23: Getting the UnityEngine.AI library and declaring all needed variables<\/p>\n<p>Now, in the <strong>Start<\/strong> function, you&#8217;ll need to enter a single line of code that will allow you to get a reference to the <em>Nav Mesh Agent <\/em>component at the very start of the game. Simply enter the following code into the <strong>Start <\/strong>function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">nav = GetComponent&lt;NavMeshAgent&gt;();<\/pre>\n<p>Next, go to the <em>Update <\/em>function. Before entering anything in this function, rename it to <strong><em>FixedUpdate<\/em><\/strong>. The difference between <em>Update <\/em>and <strong><em>FixedUpdate<\/em><\/strong> is that <em>Update <\/em>will run once every frame, whereas <strong><em>FixedUpdate<\/em><\/strong> is capable of running zero, one, or more times per frame. If, for example, your game is running at thirty frames per second, your <strong><em>FixedUpdate<\/em><\/strong> can make sure physics calculations are consistent and in synch with the global physics timestep of your game. Thus, it is recommended that any physics updates such as movement should be placed within the <strong><em>FixedUpdate<\/em><\/strong>. With that in mind, add the following code to the <strong><em>FixedUpdate<\/em><\/strong> function.<\/p>\n<pre class=\"lang:c# theme:vs2012\">if (!nav.pathPending &amp;&amp; nav.remainingDistance &lt; 0.5f)\r\n  \tGoToNextPoint();<\/pre>\n<p>Here you&#8217;re checking to see if the object has reached its destination or not. If it has not reached its destination (the next point in its list), then nothing happens. However, if it has reached its destination, it will call the <strong>GoToNextPoint<\/strong> method to find the next point to travel to. By now your <strong>Start<\/strong> and <strong>FixedUpdate<\/strong> functions should look like Figure 24.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"238\" class=\"wp-image-77883\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-37.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 24: The Start and FixedUpdate functions<\/p>\n<p>Finally, the <strong>GoToNextPoint<\/strong> method must be created. Underneath the <strong>FixedUpdate<\/strong><em>, <\/em>function enter the following code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">void GoToNextPoint()\r\n  {\r\n  \tif (points.Length == 0)\r\n  \t{\r\n  \t\treturn;\r\n  \t}\r\n  \tnav.destination = points[destPoint].position;\r\n  \tdestPoint = (destPoint + 1) % points.Length;\r\n  }<\/pre>\n<p>As stated above, the <strong>GoToNextPoint<\/strong> method will find the next point for the object to travel to. However, if the <strong>points<\/strong> array has nothing in it, it will instead cancel the method execution. This can help prevent any accidental errors from not entering anything into the array. After checking the length of the <strong>points<\/strong> array, it will then go on to set <em>Object&#8217;s <\/em>next destination and find the next point in the list to have ready for the next time this method is called.<\/p>\n<p>Once this method has been written, this script will be complete. Figure 25 shows the completed code along with the code for the <strong>GoToNextPoint <\/strong>method. Save the script and return to the Unity editor.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"489\" height=\"597\" class=\"wp-image-77884\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-38.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 25: The completed Pathfinding script<\/p>\n<h2>Completing the Project<\/h2>\n<p>To finish the <em>Pathfinding <\/em>project, you simply need to create several points for the <em>Object <\/em>to travel to. Once again, click the <em>Create <\/em>button in the <em>Hierarchy <\/em>and select <em>3D Object-&gt;Cube<\/em> to create a new cube object. Make sure its <em>Y Position <\/em>value is set to 1 (<em>X <\/em>and <em>Z <\/em>can be any value, so long as it&#8217;s within the map you created) in the<em> Inspector. A<\/em>lso disable the <em>Box Collider <\/em>component by clicking the checkbox next to the name of the component. Set the X, Y, and Z scale to 0.25.<\/p>\n<p>With your new <em>Point <\/em>object in place, create a new material to help identify the points. Name this material <em>Point <\/em>and give it whatever color you wish (the example will use green). Assign the <em>Point <\/em>material to the <em>Point <\/em>object like you did with the <em>Floor <\/em>object and material. Finally, set the <em>X, Y, <\/em>and <em>Z <\/em>scale of 0.25. The properties of the Point object should look similar to Figure 26.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"310\" height=\"499\" class=\"wp-image-77885\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-135.png\" \/><\/p>\n<p class=\"caption\">Figure 26: The Point object properties<\/p>\n<p>Next, create duplicates of the <em>Point <\/em>object by selecting <em>Point <\/em>in the <em>Hierarchy <\/em>and pressing <strong>Ctrl+D<\/strong>. You can have as many <em>Point <\/em>objects as you wish, but this example will have a total of five <em>Point <\/em>objects. Once these objects are created, move each <em>Point <\/em>object to a different position on the map. Figure 27 shows an example of what the map with points will look like. Remember that yours may not look exactly like the image, depending on how you choose to set up your scene.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1001\" height=\"486\" class=\"wp-image-77886\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-39.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 27: Points with new material and size<\/p>\n<p>Now you&#8217;ll just need to set the <em>Point <\/em>objects as the values of the <em>Points <\/em>array created in your <em>Pathfinding <\/em>script. Select <em>Object <\/em>and lock the <em>Inspector <\/em>window onto <em>Object<\/em> so that you don&#8217;t accidentally change screens when selecting other objects. Figure 28 shows where to click to lock the window.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"128\" height=\"90\" class=\"wp-image-77887\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-40.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 28: Locking the Inspector window<\/p>\n<p>Select all the <em>Point <\/em>objects in the <em>Hierarchy <\/em>and drag them into the <em>Points <\/em>field in the <em>Pathfinding <\/em>component like in Figure 29.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"889\" height=\"854\" class=\"wp-image-77888\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-41.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 29: Setting the values of the Points array<\/p>\n<p>Finally, you should reorient the camera, so you can see what <em>Object<\/em> is doing when you press the play button. Be sure to unlock the <em>Inspector <\/em>window, then choose the <em>Main Camera <\/em>object from the <em>Hierarchy <\/em>and set the following values:<\/p>\n<ul>\n<li>Position X: 0<\/li>\n<li>Position Y: 20<\/li>\n<li>Position Z: 0<\/li>\n<li>Rotation X: 90<\/li>\n<\/ul>\n<p>When finished, the <em>Transform <\/em>component for your <em>Main Camera <\/em>object should look like the one seen in Figure 30.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"579\" height=\"138\" class=\"wp-image-77889\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-42.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 30: Setting up the Main Camera rotation and position<\/p>\n<p>Once you have finished setting up the <em>Main Camera<\/em> object, press the play button at the top of the Unity editor and watch <em>Object <\/em>move to the different points you set up for it. When you&#8217;re done watching, click the play button at the top again or press <strong>Ctrl+P<\/strong> to exit play mode. Rearrange the points to create a new path for your object and play the game again. Notice how it both consistently finds the fastest route and is smart enough to avoid the walls you created, regardless of your point layout. Figure 31 shows this project in action.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1472\" height=\"692\" class=\"wp-image-77890\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/03\/word-image-43.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 31: The Pathfinding project in action.<\/p>\n<h2>Conclusion<\/h2>\n<p>With the project completed you have learned how to create a navigation mesh in your scene and have an object look at that navmesh to figure out where it can and cannot go. Unity makes it easy to quickly create this navmesh as well as put together a simple pathfinding system for an object. You can apply this knowledge in virtually any type of game, be it a stealth game where you assign enemy patrol routes for the player to avoid or in a puzzle game where you can decide where certain parts of a puzzle move to. The Unity engine handles a lot of the heavy lifting for you, permitting you to put more time into making a fun game without getting bogged down in trying to create simple AI.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unity makes it simple to create an object that follows a path. In this article, game programmer Lance Talbert shows how to use C# to control the object&#8217;s path.&hellip;<\/p>\n","protected":false},"author":317499,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[],"coauthors":[52549],"class_list":["post-77860","post","type-post","status-publish","format-standard","hentry","category-dotnet-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/77860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/317499"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=77860"}],"version-history":[{"count":8,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/77860\/revisions"}],"predecessor-version":[{"id":78315,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/77860\/revisions\/78315"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=77860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=77860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=77860"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=77860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}