<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: admoya</title>
    <description>The latest articles on DEV Community by admoya (@admoya).</description>
    <link>https://dev.to/admoya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F926972%2Fb660fa82-d943-43da-a14c-a08c30d40d00.png</url>
      <title>DEV Community: admoya</title>
      <link>https://dev.to/admoya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/admoya"/>
    <language>en</language>
    <item>
      <title>Godot 3.5 2D Navigation Tutorial</title>
      <dc:creator>admoya</dc:creator>
      <pubDate>Thu, 15 Sep 2022 18:58:25 +0000</pubDate>
      <link>https://dev.to/admoya/godot-35-2d-navigation-tutorial-31kc</link>
      <guid>https://dev.to/admoya/godot-35-2d-navigation-tutorial-31kc</guid>
      <description>&lt;p&gt;Learn the new Navigation system in Godot 3.5 by creating a city map with cars and pedestrians navigating the streets and sidewalks! Here's a quick video of what we'll have by the end of this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://godotengine.org/article/godot-3-5-cant-stop-wont-stop" rel="noopener noreferrer"&gt;Godot 3.5 is out&lt;/a&gt;, and it's awesome. Perhaps the biggest feature for 2D games is the reworked navigation system, which has been ported over from the upcoming 4.0 release. Among the benefits of the new system are a unified navigation server which removed the need for dedicated &lt;code&gt;Navigation2D&lt;/code&gt; nodes and the introduction of the &lt;code&gt;NavigationAgent2D&lt;/code&gt;, which abstracts and simplifies tedious navigation tasks such as pathfinding and collision avoidance. &lt;/p&gt;

&lt;p&gt;In this tutorial, we will construct the city of &lt;code&gt;GodotTown&lt;/code&gt; using free assets from the incomparable &lt;a href="https://www.kenney.nl/" rel="noopener noreferrer"&gt;Kenney&lt;/a&gt;. Within our city we will have roads and sidewalks, with cars and pedestrians navigating on them. We'll implement pathfinding with and without using &lt;code&gt;NavigationAgent2D&lt;/code&gt; so that we can compare the pros and cons. &lt;/p&gt;

&lt;p&gt;The first few sections will be about designing and building the level, and will mostly deal with &lt;code&gt;TileMaps&lt;/code&gt;. If you already know this stuff and just want to see the new navigation features, skip down to Adding Navigation for Cars.&lt;/p&gt;

&lt;p&gt;By end of this guide, we hope you have a better understanding of 2D Navigation in Godot 3.5, and that you're inspired to make some awesome games using it!&lt;/p&gt;

&lt;p&gt;Here are the prerequisites: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download &lt;a href="https://godotengine.org/download/linux" rel="noopener noreferrer"&gt;Godot 3.5&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://git-scm.com/doc" rel="noopener noreferrer"&gt;Clone&lt;/a&gt;/Download &lt;a href="https://github.com/CSMA-Technology/godot-town" rel="noopener noreferrer"&gt;this project from Github&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;This repo starts as an empty 2D Godot scene, with nothing but the assets we are going to use inside of an &lt;code&gt;assets&lt;/code&gt; folder&lt;/li&gt;
&lt;li&gt;Each branch within the repo corresponds with the start of a section in this tutorial (starting with &lt;code&gt;step-1/creating-the-tilemap&lt;/code&gt;). We will have instructions below for skipping steps. If you know nothing about Git and this branch thing sounds weird to you, don't worry! Just download the project and follow along, you don't have to touch any of that. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Creating the Tilemap
&lt;/h2&gt;

&lt;p&gt;In this section, we will be creating the foundation of our game using two &lt;a href="https://docs.godotengine.org/en/stable/classes/class_tileset.html" rel="noopener noreferrer"&gt;TileSets&lt;/a&gt;, that we will use in &lt;a href="https://docs.godotengine.org/en/stable/classes/class_tilemap.html" rel="noopener noreferrer"&gt;TileMaps&lt;/a&gt; for Roads and Sidewalks to build the cityscape of our level. It will look like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FPvo4fJs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FPvo4fJs.png" alt="the final TileMaps"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt;: Add two &lt;code&gt;TileMaps&lt;/code&gt; to the Main Scene (one for roads and one for sidewalks), and create &lt;code&gt;TileSets&lt;/code&gt; using &lt;code&gt;tilemap_packed.png&lt;/code&gt;.&lt;br&gt;
If you want to skip this section and move on to Adding Cars and Pedestrians, run this command &lt;code&gt;git checkout step-2/adding-cars-and-pedestrians&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A &lt;code&gt;TileMap&lt;/code&gt; is a grid of tiles used to quickly and easily paint a game's layout or background. &lt;code&gt;TileMaps&lt;/code&gt; have a lot of useful features which make them super powerful to use when building the map of your game: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can quickly place a large amount of different types of tiles within the viewport&lt;/li&gt;
&lt;li&gt;You can auto-arrange and add a level of randomness to the tiles to make it look more natural (e.g. different planks across a wooden floor) using &lt;a href="https://docs.godotengine.org/en/stable/tutorials/2d/using_tilemaps.html#autotiles" rel="noopener noreferrer"&gt;AutoTiles&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;You can add collision, occlusion, and most importantly for this tutorial, navigation to each tile&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;TileMaps&lt;/code&gt; have &lt;code&gt;TileSets&lt;/code&gt;, which are resources that hold all the properties of the tiles.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This is not a &lt;code&gt;TileMap&lt;/code&gt; tutorial, so we won't really be going into too much detail about them. If you're not familiar with them, just go through the motions and follow our instructions exactly, and you can focus on Navigation. (Alternatively, scroll back up to the TLDR and follow the instructions to skip to the next section.) &lt;br&gt;
Check out the official Godot Tutorial on &lt;a href="https://docs.godotengine.org/en/stable/tutorials/2d/using_tilemaps.html#autotiles" rel="noopener noreferrer"&gt;Using TileMaps&lt;/a&gt; for more info.&lt;/p&gt;

&lt;p&gt;If you'd like us to create a tutorial on &lt;code&gt;TileMaps&lt;/code&gt; (or any other topics), please let us know by commenting below!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First, add two &lt;code&gt;TileMap&lt;/code&gt; nodes to the Main Scene, naming one &lt;code&gt;Roads&lt;/code&gt; and the other &lt;code&gt;Sidewalks&lt;/code&gt;. Technically, we could draw the map with one &lt;code&gt;TileMap&lt;/code&gt;, but separate &lt;code&gt;TileMaps&lt;/code&gt; are necessary here for the Navigation we want to achieve. More on this in the coming sections!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FSpboprZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FSpboprZ.png" alt="two TileMap nodes in our scene"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we'll create the &lt;code&gt;TileSets&lt;/code&gt; for each of the &lt;code&gt;TileMaps&lt;/code&gt;, first for the Roads. Click on the &lt;code&gt;Roads&lt;/code&gt; node in the scene tree, then in the &lt;strong&gt;Inspector&lt;/strong&gt; find the &lt;code&gt;Tile Set&lt;/code&gt; property, which should be empty. &lt;/p&gt;

&lt;p&gt;In the dropdown, select &lt;strong&gt;New TileSet&lt;/strong&gt;, then click on the &lt;code&gt;TileSet&lt;/code&gt; and the &lt;strong&gt;TileSet Editor&lt;/strong&gt; should pop up at the bottom (where the console and debugger are located). It's easiest to deal with &lt;code&gt;TileSets&lt;/code&gt; in a larger view, so we'll expand the menu by clicking on this button at the bottom of the right hand hand corner of the editor: &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2Fzwvw2JY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2Fzwvw2JY.png" alt="the "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, find &lt;code&gt;tilemap_packed.png&lt;/code&gt; in the &lt;code&gt;assets&lt;/code&gt; folder and drag it to the left panel of the &lt;strong&gt;TileSet Editor&lt;/strong&gt;. &lt;/p&gt;



&lt;p&gt;Now create a single tile by clicking &lt;strong&gt;New Single Tile&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To make sure our selections are accurate, we want grid snapping. To turn it on, click on the &lt;strong&gt;Snap To Grid&lt;/strong&gt; button &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FJ8Qw17o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FJ8Qw17o.png" alt="Snap to Grid button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make any selection by clicking anywhere on the tileset. The menu should then open in the right panel. Find the properties under &lt;strong&gt;Snap Options&lt;/strong&gt; and set &lt;code&gt;Step&lt;/code&gt; to &lt;code&gt;x: 16 y:16&lt;/code&gt;. Now we can easily select the upper left-hand corner of the first road tile (towards the bottom left of the spritesheet), which has a crosswalk print on it.&lt;/p&gt;



&lt;p&gt;We can continue to create single tiles for the rest of the road; however, instead we're going to create an &lt;a href="https://docs.godotengine.org/en/stable/tutorials/2d/using_tilemaps.html#atlas-tiles" rel="noopener noreferrer"&gt;Atlas&lt;/a&gt;, which allows us to create multiple tiles at once by selecting a rectangular region of the spritesheet. &lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;New Atlas&lt;/strong&gt; at the top and drag across all the road tiles in the bottom left of the spritesheet. Once done, expand &lt;strong&gt;Selected Tile&lt;/strong&gt; in the &lt;strong&gt;Inspector&lt;/strong&gt;, and we should see &lt;code&gt;Tile Mode&lt;/code&gt; set to &lt;code&gt;ATLAS_TILE&lt;/code&gt; and &lt;code&gt;Subtile Size&lt;/code&gt; set to &lt;code&gt;x:16 y:16&lt;/code&gt;. Change the &lt;code&gt;Name&lt;/code&gt; of the atlas, so we can see it clearly in the &lt;code&gt;TileMap&lt;/code&gt; menu later on.&lt;/p&gt;



&lt;p&gt;Once that's done, we can go back to the &lt;strong&gt;2D Scene Editor&lt;/strong&gt; and click on the &lt;code&gt;Roads&lt;/code&gt; node again. &lt;/p&gt;

&lt;p&gt;Before drawing our map, set the &lt;code&gt;cell_size&lt;/code&gt; to match our &lt;code&gt;step&lt;/code&gt; in the &lt;code&gt;TileSet&lt;/code&gt;. In the &lt;strong&gt;Inspector&lt;/strong&gt;, under &lt;strong&gt;Cell&lt;/strong&gt; set the &lt;code&gt;Size&lt;/code&gt; to &lt;code&gt;x: 16 y: 16&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FEYRLBvv.png" class="article-body-image-wrapper"&gt;&lt;img alt="the cell size in the inspector" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FEYRLBvv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can draw our road by clicking on the &lt;code&gt;roads&lt;/code&gt; atlas from the Tilemap menu, and then selecting the tiles we want to paint. &lt;/p&gt;



&lt;p&gt;Repeat the process for &lt;code&gt;Sidewalks&lt;/code&gt;. We actually used &lt;a href="https://docs.godotengine.org/en/stable/tutorials/2d/using_tilemaps.html#autotiles" rel="noopener noreferrer"&gt;AutoTiles&lt;/a&gt; for our Sidewalks, but another atlas will work as well! After creating the &lt;code&gt;TileSet&lt;/code&gt;, draw the sidewalks as well -- optionally, you can add more &lt;code&gt;TileMaps&lt;/code&gt; for buildings and decorations to liven up your town. &lt;/p&gt;

&lt;p&gt;Now we should have a full map like below!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: if you notice a faint gray line in between tiles on your finished map, you may need to re-import your spritesheet. By default, Godot uses "filter" mode to import the files, but you can turn it off. Click on &lt;code&gt;tilemap_packed.png&lt;/code&gt; and go to the &lt;strong&gt;Import&lt;/strong&gt; tab at the top of the left-hand panel. Find the &lt;code&gt;Filter&lt;/code&gt; property and make sure it is unchecked, then click &lt;strong&gt;Reimport&lt;/strong&gt;, and those ugly gray lines should disappear!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FyAuyNmz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FyAuyNmz.jpg" alt="the finished TileMaps"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome, now we have our map! In the next section, we'll be adding cars and people to the level and scripting them to move around the town.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Adding Cars and Pedestrians
&lt;/h2&gt;

&lt;p&gt;Now that we have some roads and sidewalks, we can add cars and pedestrians, each trying to get somewhere. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TLDR:&lt;/strong&gt; Create two scenes with &lt;code&gt;KinematicBody2D&lt;/code&gt; as the root node, and name one &lt;code&gt;Car&lt;/code&gt; and the other &lt;code&gt;Pedestrian&lt;/code&gt;. Assign them a sprite from the assets folder and a collision shape, then make a script that they will both share that moves them towards a goal. &lt;br&gt;
To skip to the next section and move on to Adding Navigation for Cars, run this command &lt;code&gt;git checkout step-3/adding-navigation-for-cars&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cars and pedestrians will work in basically the same way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They will be assigned a goal, which is a &lt;code&gt;Node2D&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;They will move towards that goal at a designated &lt;code&gt;speed&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;They will &lt;code&gt;queue_free&lt;/code&gt; when they hit the goal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll be using a &lt;a href="https://docs.godotengine.org/en/stable/classes/class_kinematicbody2d.html#class-kinematicbody2d" rel="noopener noreferrer"&gt;&lt;code&gt;KinematicBody2D&lt;/code&gt;&lt;/a&gt; node for both of these, which is a very important node for 2D games! If you're not familiar with it, make sure to check out &lt;a href="https://docs.godotengine.org/en/stable/tutorials/physics/kinematic_character_2d.html" rel="noopener noreferrer"&gt;this tutorial&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;For our first draft, cars and pedestrians will ignore any navigation and just move in a straight line towards the goal, disappearing when they get there. &lt;/p&gt;

&lt;p&gt;Let's start by creating our &lt;code&gt;Car&lt;/code&gt; scene. Create a new child in our main scene, a &lt;code&gt;KinematicBody2D&lt;/code&gt; node, and then configuring it in-place and saving the branch as a scene. This is an alternative to creating a new empty scene, and it has the advantage of allowing us to visualize our car on the scene it will ultimately be in.&lt;/p&gt;

&lt;p&gt;Rename the &lt;code&gt;KinematicBody2D&lt;/code&gt; to &lt;code&gt;Car&lt;/code&gt;. Then create a child &lt;code&gt;Sprite&lt;/code&gt; node, and assign that &lt;code&gt;Sprite&lt;/code&gt; &lt;code&gt;tilemap_packed.png&lt;/code&gt; as its texture. Don't worry, Godot's &lt;code&gt;Sprite&lt;/code&gt; nodes have a handy way to crop the texture, and we're going to use that to get just the portion of the image that has a car. &lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Inspector&lt;/strong&gt; for the &lt;code&gt;Sprite&lt;/code&gt; node, expand the &lt;strong&gt;Region&lt;/strong&gt; section, enable it, and set the &lt;code&gt;Rect&lt;/code&gt; values to: &lt;code&gt;x: 240 y: 230 w: 32 h: 32&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FPTwmhXA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FPTwmhXA.png" alt="a yellow car"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And there we have our car! Move the car to somewhere on the roads, and make sure you are moving the &lt;code&gt;Car&lt;/code&gt; node and not just the sprite! &lt;/p&gt;

&lt;p&gt;Now just add a &lt;code&gt;CollisionShape2D&lt;/code&gt; as a child of the &lt;code&gt;Car&lt;/code&gt; node, give it a new &lt;code&gt;RectangleShape2D&lt;/code&gt; from the &lt;code&gt;Shape&lt;/code&gt; property in the &lt;strong&gt;Inspector&lt;/strong&gt;, and set the &lt;code&gt;Extents&lt;/code&gt; to &lt;code&gt;x: 10 y: 10&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Before we get to the script, let's make goals for the cars and pedestrians to move towards. Create two new &lt;code&gt;Node2D&lt;/code&gt;s at the top level of the main scene, and rename them to &lt;code&gt;CarGoal&lt;/code&gt; and &lt;code&gt;PedestrianGoal&lt;/code&gt;. Drag them to nice spots on the road and sidewalks, respectively.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F4uaVhx6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F4uaVhx6.png" alt="example locations of the goals"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a little bonus, we're going to use a new feature introduced in Godot 3.5, &lt;a href="https://docs.godotengine.org/en/stable/tutorials/scripting/scene_unique_nodes.html" rel="noopener noreferrer"&gt;Scene Unique Names&lt;/a&gt;! Just right-click on our two goal nodes and tick the box that says &lt;strong&gt;% Access as Scene Unique Name&lt;/strong&gt;:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.godotengine.org%2Fen%2Fstable%2F_images%2Funique_name.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdocs.godotengine.org%2Fen%2Fstable%2F_images%2Funique_name.png" alt="The Scene Unique Name option"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is going to enable us to refer to the goals in our code by their name, instead of by their relative location in the scene. &lt;/p&gt;

&lt;p&gt;Now add a script to the &lt;code&gt;Car&lt;/code&gt; node and call it &lt;code&gt;Movable.gd&lt;/code&gt;. Let's start by exporting some variables that we know we'll need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gdscript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;goal&lt;/span&gt; &lt;span class="c1"&gt;# Our scene unique name&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;current_speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="c1"&gt;# The speed we should move at (pixels per second)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;arrival_tolerance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;# How close to a point do we get before we are there&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's pause and explain &lt;code&gt;arrival_tolerance&lt;/code&gt;. When we eventually implement pathfinding, our cars will get an array of points to follow in sequence. Our script is then going to say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Try to get to point &lt;strong&gt;X&lt;/strong&gt;. If we are within &lt;code&gt;arrival_tolerance&lt;/code&gt; of point &lt;strong&gt;X&lt;/strong&gt;, we can consider ourselves there and move on to the next one. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Without this, we would need to perfectly line up our center point at each position along the path, which may not always be possible (for example, if there is a collision preventing us from getting exactly there). &lt;/p&gt;

&lt;p&gt;We have our &lt;code&gt;goal&lt;/code&gt; export variable as a string, which will be a scene unique name. We need to find the position of that node when we start up. So let's add the following &lt;a href="https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html" rel="noopener noreferrer"&gt;&lt;code&gt;onready&lt;/code&gt;&lt;/a&gt; variable right under the &lt;code&gt;goal&lt;/code&gt; declaration:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;onready var goal_pos = get_node(goal).global_position&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, we'll write a simple movement function using &lt;a href="https://docs.godotengine.org/en/stable/classes/class_kinematicbody2d.html#class-kinematicbody2d-method-move-and-collide" rel="noopener noreferrer"&gt;move_and_collide&lt;/a&gt;, which we will run at every physics frame:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gdscript"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;move_towards_goal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;speed&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Get a unit-length vector pointing in the direction we want to go&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal_pos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;global_position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# Apply our speed to the direction vector to get our movement vector&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt;
    &lt;span class="c1"&gt;# Move our KinematicBody2D node&lt;/span&gt;
    &lt;span class="n"&gt;move_and_collide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And &lt;em&gt;finally&lt;/em&gt;, call our movement function within &lt;code&gt;_physics_process&lt;/code&gt;. We are using this lifecycle method instead of &lt;code&gt;_process&lt;/code&gt;, because &lt;code&gt;_physics_process&lt;/code&gt; runs at consistent intervals, making movement smooth and independent of framerate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gdscript"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_physics_process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# When we reach our goal (within tolerance), queue_free&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;global_position&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal_pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;arrival_tolerance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;queue_free&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# If we are not at our goal, keep moving!&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;move_towards_goal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;current_speed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One note about the above script: &lt;code&gt;delta&lt;/code&gt; is the time (in seconds) since the last time &lt;code&gt;_physics_process&lt;/code&gt; was called (i.e., since the last "Physics Frame"). However, since &lt;code&gt;_physics_process&lt;/code&gt; runs at fixed intervals, it should always be the same. Even so, multiplying by that value gives us the desired behavior of &lt;code&gt;speed&lt;/code&gt; being expressed in pixels per second.&lt;/p&gt;

&lt;p&gt;And there we have it, our &lt;code&gt;Car&lt;/code&gt; is complete, and our script (sans comments) should be looking like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gdscript"&gt;&lt;code&gt;&lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;KinematicBody2D&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;goal&lt;/span&gt;
&lt;span class="k"&gt;onready&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;goal_pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;global_position&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;current_speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;arrival_tolerance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_physics_process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;global_position&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal_pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;arrival_tolerance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;queue_free&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;move_towards_goal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;current_speed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;move_towards_goal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;speed&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal_pos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;global_position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt;

    &lt;span class="n"&gt;move_and_collide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before saving the car as a scene, let's add a value for the &lt;code&gt;Goal&lt;/code&gt; property. Click the &lt;code&gt;Car&lt;/code&gt; and then type in the scene unique name of our goal (&lt;code&gt;%CarGoal&lt;/code&gt;) into the &lt;strong&gt;Inspector&lt;/strong&gt; field, including the percentage sign.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FE4UzRdi.png" class="article-body-image-wrapper"&gt;&lt;img alt="the Goal name in the inspector" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FE4UzRdi.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, right-click on the &lt;code&gt;Car&lt;/code&gt; node and select &lt;strong&gt;Save Branch as Scene&lt;/strong&gt;. If you run the scene here, the car should move towards the goal!&lt;/p&gt;

&lt;p&gt;Adding pedestrians will follow the exact same process, and it should be a snap. &lt;/p&gt;

&lt;p&gt;First, add a &lt;code&gt;KinematicBody2D&lt;/code&gt; to the main scene, and rename it to &lt;code&gt;Pedestrian&lt;/code&gt;. Add a &lt;code&gt;Sprite&lt;/code&gt; to it, and give it the same &lt;code&gt;tilemap_packed.png&lt;/code&gt; texture. This time, here are the magic numbers for the &lt;code&gt;Region&lt;/code&gt; property:&lt;br&gt;
&lt;code&gt;x: 384 y: 0 w: 16 h: 16&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FCOw37Sv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FCOw37Sv.png" alt="a dude"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give the &lt;code&gt;Pedestrian&lt;/code&gt; a &lt;code&gt;CollisionShape2D&lt;/code&gt;. We can select a &lt;code&gt;RectangleShape2D&lt;/code&gt; again, and give it the following extents: &lt;code&gt;x: 5 y: 8&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we can just grab our &lt;code&gt;Movable.gd&lt;/code&gt; script from the &lt;strong&gt;FileSystem&lt;/strong&gt; section and drag it onto the &lt;code&gt;Pedestrian&lt;/code&gt;. Before we save to a scene, assign a value to the &lt;code&gt;Goal&lt;/code&gt; export value in the &lt;strong&gt;Inspector&lt;/strong&gt;; this time it should be &lt;code&gt;%PedestrianGoal&lt;/code&gt;. Save our dude as a scene by right-clicking and selecting &lt;strong&gt;Save Branch as Scene&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Before we run the scene, let's add a little organization so we can add a bunch of cars and pedestrians. Make two new &lt;code&gt;Nodes&lt;/code&gt; in the scene, and name them &lt;code&gt;Cars&lt;/code&gt; and &lt;code&gt;People&lt;/code&gt;. Drag the &lt;code&gt;Car&lt;/code&gt; node into &lt;code&gt;Cars&lt;/code&gt; and the &lt;code&gt;Pedestrian&lt;/code&gt; node into... well, you get it. &lt;/p&gt;

&lt;p&gt;Now just click on the &lt;code&gt;Car&lt;/code&gt; and/or &lt;code&gt;Pedestrian&lt;/code&gt; and hit &lt;strong&gt;Ctrl + D&lt;/strong&gt; to duplicate them as many time as you want. You can drag them around to different positions on the map, and modulate their colors (&lt;strong&gt;Inspector&lt;/strong&gt; -&amp;gt; &lt;strong&gt;CanvasItem&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Visibility&lt;/strong&gt; -&amp;gt; &lt;code&gt;Modulate&lt;/code&gt; ) to add some diversity to GodotTown! You can also turn the visibility of the &lt;code&gt;CollisionShape2D&lt;/code&gt; off (in the &lt;code&gt;Pedestrian&lt;/code&gt; and/or &lt;code&gt;Car&lt;/code&gt; scene) to see the colors more easily. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FEmUmPMr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FEmUmPMr.png" alt="A hidden CollisionShape2D"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FkZDoFQ4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FkZDoFQ4.png" alt="GodotTown with some residents"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's run the scene and see what happens...&lt;/p&gt;



&lt;p&gt;We can see that cars are driving all over the sidewalk (and in the void of space!), the pedestrians are jaywalking, everyone is crashing into each other, it's madness! In the next step, we are going to add navigation and use navigation layers to make sure pedestrians only walk on the sidewalks and cars only drive on roads. &lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: Adding Navigation for Cars
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TLDR:&lt;/strong&gt; Add navigation polygons to our road tiles, and then change the &lt;code&gt;Movable.gd&lt;/code&gt; script to make a path with &lt;code&gt;Navigation2DServer&lt;/code&gt;.&lt;br&gt;
To skip to the next section and move on to Adding Navigation for Pedestrians, run this command &lt;code&gt;git checkout step-4/adding-navigation-for-pedestrians&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we will add navigation to our &lt;code&gt;Roads&lt;/code&gt; &lt;code&gt;TileMap&lt;/code&gt;! We will define a region on each of the tiles we use that Godot will consider navigable, and when those regions match up along the edge, they will be merged into one larger navigable area. This will allow the &lt;code&gt;NavigationServer2D&lt;/code&gt; to plot a path between any two points on the contiguous region. &lt;/p&gt;

&lt;p&gt;First, let's click on our &lt;code&gt;Roads&lt;/code&gt; &lt;code&gt;TileMap&lt;/code&gt;, open the &lt;strong&gt;Tileset Editor&lt;/strong&gt; from the &lt;strong&gt;Inspector&lt;/strong&gt;, and expand it. With a left click on a road tile, or by clicking the left/right arrows on the top of the editor, select the &lt;code&gt;Roads&lt;/code&gt; atlas we defined earlier. You'll know you have the right one when you see the correct &lt;code&gt;Name&lt;/code&gt; property under &lt;strong&gt;Selected Tile&lt;/strong&gt; in the &lt;strong&gt;Inspector&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FdbK2vA6.png" class="article-body-image-wrapper"&gt;&lt;img alt="the tile setting int he inspector" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FdbK2vA6.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Navigation&lt;/strong&gt; from the top of the Tileset Editor, and then click &lt;strong&gt;New Rectangle&lt;/strong&gt; from the row underneath, or hit &lt;strong&gt;Shift + R&lt;/strong&gt;. Click into the tile you had selected and you'll see it light up in a light green color; that is a navigation region! &lt;/p&gt;



&lt;p&gt;Note that what we just did marked the &lt;strong&gt;entire&lt;/strong&gt; tile as navigable. We could just mark specific parts within the tile as navigable instead by turning off snapping (not recommended) or changing the values of &lt;code&gt;Snap Options -&amp;gt; Step&lt;/code&gt; in the &lt;strong&gt;Inspector&lt;/strong&gt;. This would allow us to do something like excluding the edges of tiles from navigation, which would be useful because the path that is ultimately drawn will be for the &lt;em&gt;center&lt;/em&gt; of the car, which means if it runs along the edge, the car will be halfway off the road. We do want to keep our cars off the edge, but we will do so in a simpler way: by only marking the tiles we use for the middle of the road as navigable. &lt;/p&gt;

&lt;p&gt;After clicking through the tiles and assigning navigation regions to the middle tiles, we are left with this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2Fp3cxdGx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2Fp3cxdGx.png" alt="our selected tiles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A note and a warning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Note&lt;/strong&gt;: Our specific spritesheet/tiles makes this process very easy because we are marking entire tiles as navigable, but in future projects we can use either the &lt;strong&gt;Rectangle&lt;/strong&gt; (&lt;code&gt;Shift + R&lt;/code&gt;) or &lt;strong&gt;Polygon&lt;/strong&gt; (&lt;code&gt;Shift + P&lt;/code&gt;) tool to draw the navigation on only a portion of each tile. (&lt;strong&gt;Note:&lt;/strong&gt; we actually did ) The Rectangle Tool works by clicking-and-dragging a rectangle, and the Polygoon Tool works by clicking on points that will be vertices of the polygon. In both cases, it is helpful to enable grid snapping &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FJ8Qw17o.png" alt="Snap to Grid Button"&gt; and adjust the &lt;code&gt;Step&lt;/code&gt; in the &lt;strong&gt;Inspector&lt;/strong&gt; under &lt;code&gt;Snap Options&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Warning&lt;/strong&gt;: When using the &lt;strong&gt;Polygon Tool&lt;/strong&gt; for navigation, the first point we click on will &lt;strong&gt;not&lt;/strong&gt; show up as a red dot. It is not until the second point is clicked that both points display. If we double-click the same first point because we thought there was no vertex there, it will create two overlapping points, which will cause errors down the road. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we proceed, let's run the game and see our changes. In order to view the navigation region we will need to do two things: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click on the &lt;code&gt;Roads&lt;/code&gt; &lt;code&gt;TileMap&lt;/code&gt; node and in the &lt;strong&gt;Inspector&lt;/strong&gt; select &lt;strong&gt;Navigation -&amp;gt; Bake Navigation&lt;/strong&gt;. This is required for navigation to work, but is off by default because not all &lt;code&gt;TileMap&lt;/code&gt; nodes will have navigation regions.&lt;/li&gt;
&lt;li&gt;In the top menu of Godot, select &lt;strong&gt;Debug -&amp;gt; Visible Navigation&lt;/strong&gt;. This will highlight navigation regions in a light transparent green when we run the game. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now when we run the game, we will see the highlighted navigation regions, which should form a coherent, drivable space for our cars. Of course, we haven't changed their script yet, so the behavior of the cars and people will be unchanged. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FlT0wZ50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FlT0wZ50.png" alt="the map with navigation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's put that navigation to good use. First, let's assign some &lt;code&gt;Navigation Layers&lt;/code&gt; to the two navigable &lt;code&gt;TileMaps&lt;/code&gt;. These layers will enable us to separate adjacent navigable regions (the roads and the sidewalks), so that cars will only use one, and pedestrians will only use the other. On each &lt;code&gt;TileMap&lt;/code&gt;, expand the &lt;strong&gt;Navigation&lt;/strong&gt; section in the &lt;strong&gt;Inspector&lt;/strong&gt;, and ensure that &lt;code&gt;Navigation Layers&lt;/code&gt; is set to &lt;code&gt;1&lt;/code&gt; for &lt;code&gt;Roads&lt;/code&gt;, and &lt;code&gt;2&lt;/code&gt; for &lt;code&gt;Sidewalks&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then we can crack open &lt;code&gt;Movable.gd&lt;/code&gt; and add the following export variable:&lt;br&gt;
&lt;code&gt;export (int, LAYERS_2D_NAVIGATION) var nav_layer = 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will allow us to keep sharing the same script between cars and pedestrians, while telling them they can only traverse specific &lt;code&gt;Navigation Layers&lt;/code&gt;. The &lt;code&gt;LAYERS_2D_NAVIGATION&lt;/code&gt; constant is a &lt;a href="https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_exports.html?highlight=bit%20flag#exporting-bit-flags" rel="noopener noreferrer"&gt;special export flag&lt;/a&gt; which will make the variable show up as a bit flag selector in the &lt;strong&gt;Inspector&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FbhmwYhG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FbhmwYhG.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like collisions in Godot, it is important to remember that these values are not "normal" ints, but rather, &lt;a href="https://www.hendrik-erz.de/post/bitwise-flags-are-beautiful-and-heres-why" rel="noopener noreferrer"&gt;bit flags&lt;/a&gt;. Since we will be working with just &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt; exclusively, this won't affect us much, but if we were to add, say, a cyclist who could traverse both navigation layers, then he would have both layers &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt; enabled, and that would be expressed in code as an integer with a value of &lt;code&gt;3&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now &lt;em&gt;finally&lt;/em&gt; we will get to the good part. We will modify our script to use the new &lt;a href="https://docs.godotengine.org/en/stable/classes/class_navigation2dserver.html?highlight=Navigation2dserver" rel="noopener noreferrer"&gt;&lt;code&gt;Navigation2DServer&lt;/code&gt;&lt;/a&gt; to create a path for the cars along the roads, and then follow it. And that can be done with just a few lines of code!&lt;/p&gt;

&lt;p&gt;First, add two new variables to the top of &lt;code&gt;Movable.gd&lt;/code&gt;: &lt;br&gt;
&lt;code&gt;var path = null&lt;/code&gt; and &lt;code&gt;var path_idx = 0&lt;/code&gt; that we'll use in our new solution.&lt;/p&gt;

&lt;p&gt;And then modify &lt;code&gt;move_towards_goal&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func move_towards_goal(speed):
    # If the path is null or empty, create it and return. 
    if not path:
        # map_get_path returns an array of points, ending at the goal if it is reachable
        path = Navigation2DServer.map_get_path(get_world_2d().navigation_map, global_position, goal_pos, false, nav_layer)
        # This will point to our next target along the path
        path_idx = 0
        return
    # We are using the same tolerance as before to determine if we have arrived at an intermediate point along the path
    while global_position.distance_to(path[path_idx]) &amp;lt;= arrival_tolerance:
        path_idx += 1
    # Same logic as before, but this time going to the next point in the path instead of the goal
    var direction = (path[path_idx] - global_position).normalized()
    var movement = direction * speed
    move_and_collide(movement)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's call some stuff out from that code snippet.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This function will be called every physics frame, but the navigation will not be baked and ready until the second physics frame. That is why we &lt;code&gt;return&lt;/code&gt; from the &lt;code&gt;move_towards_goal&lt;/code&gt; function after generating the path, and if the path is an empty array, it will generate again next time (because an empty array is falsy in GDScript).
&lt;/li&gt;
&lt;li&gt;The key function we are using is &lt;a href="https://docs.godotengine.org/en/stable/classes/class_navigation2dserver.html?highlight=Navigation2dserver#class-navigation2dserver-method-map-get-path" rel="noopener noreferrer"&gt;&lt;code&gt;Navigation2DServer.map_get_path&lt;/code&gt;&lt;/a&gt;. If you are familiar with the old (Godot &amp;lt;= 3.4) navigation system, this is the replacement of &lt;a href="https://docs.godotengine.org/en/stable/classes/class_navigation2d.html?highlight=get_simple_path#class-navigation2d-method-get-simple-path" rel="noopener noreferrer"&gt;&lt;code&gt;Navigation2D.get_simple_path&lt;/code&gt;&lt;/a&gt;. You'll notice two new parameters though, &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;navigation_layers&lt;/code&gt;. The &lt;code&gt;Navigation2DServer&lt;/code&gt; serves as a single object responsible for all navigation, so in order to accomodate for more complex navigation scenarios, Godot 3.5 introduces &lt;code&gt;maps&lt;/code&gt;, which contain navigation &lt;code&gt;regions&lt;/code&gt;. 

&lt;ul&gt;
&lt;li&gt;We won't be defining additional maps in this guide, but that would be one way to, for instance, have overlapping navigation regions that do not interact. Instead, we get the default navigation map with &lt;code&gt;get_world_2d().navigation_map&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We will be using the &lt;code&gt;navigation_layers&lt;/code&gt; parameter, though, which informs the &lt;code&gt;Navigation2DServer&lt;/code&gt; which layers in the given map should be considered for this path. As you can see above, we will be defining different layers for the roads and sidewalks. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;We want our cars to move along the path, which means we want them to move to one point at a time along the array returned by &lt;code&gt;map_get_path&lt;/code&gt;. Instead of modifying this array, we are using a pointer (&lt;code&gt;path_idx&lt;/code&gt;) to choose the next spot to move towards, and we are advancing that pointer when we have "arrived" according to our tolerance. &lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;That's it! Before we test it, open the &lt;code&gt;Car&lt;/code&gt; scene and make sure the &lt;code&gt;Nav Layer&lt;/code&gt; property is set to &lt;code&gt;1&lt;/code&gt; in the &lt;strong&gt;Inspector&lt;/strong&gt;. Do the same for the &lt;code&gt;Pedestrian&lt;/code&gt; scene, setting it to &lt;code&gt;2&lt;/code&gt;. Since we have not added the second &lt;code&gt;Navigation Layer&lt;/code&gt; for the &lt;code&gt;Sidewalks&lt;/code&gt; yet, we'd expect the pedestrians to stay still now. Let's run it and find out.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you run the game at this point, you may see the below error within the debugger --&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FLtWHwRp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FLtWHwRp.png" alt="get_relative_transform_to_parent errors"&gt;&lt;/a&gt;&lt;br&gt;
The navigation should still work as expected with the above instructions, so right now you can ignore this error (though it's annoying). This is a known issue in Godot 3.5 and is being fixed in an &lt;a href="https://godotengine.org/article/release-candidate-godot-3-5-1-rc-1" rel="noopener noreferrer"&gt;upcoming patch&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Looking good! Let's make those people walk now. &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Adding Navigation for Pedestrians
&lt;/h2&gt;

&lt;p&gt;Now the cars are in motion! But as you can see, when we play the scene the people are still standing in place. Let's go through how to fix that. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TLDR:&lt;/strong&gt; Add navigation regions to the &lt;code&gt;Sidewalks&lt;/code&gt;, and ensure that the export var &lt;code&gt;nav_layer&lt;/code&gt; in the &lt;code&gt;Pedestrian&lt;/code&gt; scene is set to match the &lt;code&gt;Navigation Layers&lt;/code&gt; property in &lt;code&gt;Sidewalks&lt;/code&gt;. In this tutorial, we are using layer 2.&lt;br&gt;
To skip to the next section and move on to Refactoring Pedestrians to use NavigationAgent2D, run this command &lt;code&gt;git checkout step-5/refactoring-pedestrians-to-use-navigationagent2d&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First, we'll take what we learned in the previous section and add Navigation to the Sidewalk tiles. &lt;/p&gt;

&lt;p&gt;Again, open the &lt;code&gt;TileSet&lt;/code&gt; editor by clicking on the &lt;code&gt;Sidewalks&lt;/code&gt; node in the scene tree, then clicking on the &lt;code&gt;TileSet&lt;/code&gt; selection and then expanding the menu from the bottom &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2Fzwvw2JY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2Fzwvw2JY.png" alt="the "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the individual Sidewalk tiles, or select the atlas (or Autotile set if you used that) and choose the first tile. This time, we will be adding Navigation Polygons to some of the tiles rather than just plain rectangles. The act of adding polygons is a little wonky, so bear with the process.&lt;/p&gt;

&lt;p&gt;The first block of tiles are just rectangles (but we're not including the lighter edges which are the "curbs"), so continue on like we did before for the Roads. Once those are done, select the middle group of tiles shaped like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F9ns1exv.png" class="article-body-image-wrapper"&gt;&lt;img alt="a single sidewalk tile" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F9ns1exv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Navigation&lt;/strong&gt; and then select &lt;strong&gt;New Polygon&lt;/strong&gt;, by clicking the button &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F6AlKy1b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F6AlKy1b.png" alt="the "&gt;&lt;/a&gt; &lt;br&gt;
or typing &lt;strong&gt;Shift + P&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is where it gets a little tricky. First, you'll select a starting point in the tile for your new polygon. When we click that first point, unfortunately the UI does not show any feedback or indication of where that point is, but &lt;strong&gt;make sure not to double click the point&lt;/strong&gt; -- otherwise, Godot will think that these are two separate points in the Navigation polygon and it may cause pathing to break during runtime. After clicking the starting point, move your mouse to the next corner or point of the shape, and continue on around the tile. Like the starting point, when you've finished the shape, &lt;strong&gt;do not double click the final point&lt;/strong&gt;, as this again could break pathing.&lt;/p&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If after you finish this section and run the game, you see the following error: &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F5x6gDRr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F5x6gDRr.png" alt="Polygon Tool button"&gt;&lt;/a&gt;&lt;br&gt;
It may be due to badly drawn navigation polygons -- it's best to delete all the navigation polygons on the affected &lt;code&gt;TileSet&lt;/code&gt; and carefully re-draw them. This process should be much improved in &lt;a href="https://godotengine.org/article/tiles-editor-rework" rel="noopener noreferrer"&gt;Godot 4&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Do the same for the rest of the Sidewalk tiles, so that we have the navigation areas looking like this: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FOXM88xQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FOXM88xQ.png" alt="sidewalk tiles with navigation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we are done with the nav regions in this Tileset, go back to the 2D scene editor. &lt;/p&gt;

&lt;p&gt;Now, we just need to ensure that the nav layers match up between the &lt;code&gt;Sidewalk&lt;/code&gt; tiles and the &lt;code&gt;Pedestrian&lt;/code&gt; nodes.&lt;/p&gt;

&lt;p&gt;In the properties of the &lt;code&gt;Sidewalk&lt;/code&gt; tilemap, go to the &lt;strong&gt;Navigation&lt;/strong&gt; section and ensure that only layer &lt;code&gt;2&lt;/code&gt; is selected in &lt;code&gt;Navigation Layers&lt;/code&gt;. Then, go to the &lt;code&gt;Pedestrian&lt;/code&gt; scene (not just the node, because we want to change the property for &lt;em&gt;all&lt;/em&gt; instances of &lt;code&gt;Pedestrians&lt;/code&gt;) and in the &lt;strong&gt;Inspector&lt;/strong&gt;, make sure that the &lt;code&gt;Nav Layer&lt;/code&gt; property (which corresponds to the &lt;code&gt;export var nav_layer&lt;/code&gt; in our &lt;code&gt;Movable.gd&lt;/code&gt; script) is also set to &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Awesome! Now, we should be able to run the game and see the cars &lt;strong&gt;and&lt;/strong&gt; people moving. &lt;/p&gt;



&lt;p&gt;To make things more natural, let's change the speed on some of the pedestrians, and see what happens to the pathing. Pick a couple of the pedestrians in the middle of the map and change their &lt;code&gt;Speed&lt;/code&gt; to something low, like &lt;code&gt;10&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Run the game again, and take note of how the people stack up behind the slower pedestrian, getting stuck and having to walk at the same slow speed all the way to the goal -- looks a little weird, right?&lt;/p&gt;



&lt;p&gt;Let's take a look at how to make this look more natural in the next section!&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5: Refactoring Pedestrians to use NavigationAgent2D
&lt;/h2&gt;

&lt;p&gt;We're in the home stretch! In this last section, we will refactor the code for the &lt;code&gt;Pedestrians&lt;/code&gt; to use &lt;code&gt;NavigationAgent2D&lt;/code&gt;, and really see the strengths of this newly revamped navigation system in Godot 3.5. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TLDR:&lt;/strong&gt; Add a &lt;code&gt;NavigationAgent2D&lt;/code&gt; node to the &lt;code&gt;Pedestrian&lt;/code&gt; scene. Detach the existing &lt;code&gt;Movable.gd&lt;/code&gt; script from &lt;code&gt;Pedestrian&lt;/code&gt; and create a new script that uses &lt;code&gt;NavigationAgent2D&lt;/code&gt; for navigation and collision avoidance.&lt;br&gt;
To skip this section and see the final result, run this command &lt;code&gt;git checkout final-game&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Up until now, we have been using the base &lt;code&gt;Navigation2DServer&lt;/code&gt; for the pedestrians walking along the sidewalk. This works pretty well for getting the characters from their starting point to the goal. But as you saw from the last tweak in the previous section where we made some of the people walk slower, collision avoidance isn't happening at all with just the &lt;code&gt;Navigation2DServer&lt;/code&gt;. Ideally, we'd like the people to walk around each other if they are going different speeds, just like they would in real life.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.godotengine.org/en/stable/classes/class_navigationagent2d.html" rel="noopener noreferrer"&gt;NavigationAgent2D&lt;/a&gt; abstracts away many of the pathfinding tasks we did ourselves, and also implements its own collision avoidance system. To be clear, we could accomplish everything that the Navigation Agent does manually in our own code using &lt;code&gt;Navigation2DServer&lt;/code&gt;, but we'd be writing a lot more code, and it could get messy. However, the agent is not without its complications -- here are some pros and cons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;NavigationAgent2D&lt;/code&gt; Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simplifies previously tedious code for pathfinding&lt;/li&gt;
&lt;li&gt;Easier to use for beginners in Godot Navigation&lt;/li&gt;
&lt;li&gt;Sufficient in most cases for simple navigable regions &amp;amp; obstacles&lt;/li&gt;
&lt;li&gt;Standardized approach to navigation that will increasingly become the norm -- especially upon the release of Godot 4.0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;NavigationAgent2D&lt;/code&gt; Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The collision avoidance has limitations, and it takes a lot of tweaking of the agent properties to get it looking smooth&lt;/li&gt;
&lt;li&gt;Can be hard to debug&lt;/li&gt;
&lt;li&gt;It's more of a black box and not as customizable as manually programming the navigation (i.e. we do not have the option to leave the path "unoptimized" as we did before). It's hard to achieve a certain look to your pathing with just the &lt;code&gt;NavigationAgent2D&lt;/code&gt;, leading developers to sometimes choose a different implementation altogether.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we know what &lt;code&gt;NavigationAgent2D&lt;/code&gt; can do, let's see how it will bring us to our final product.&lt;/p&gt;

&lt;p&gt;Go to the &lt;code&gt;Pedestrian&lt;/code&gt; scene and add a &lt;code&gt;NavigationAgent2D&lt;/code&gt; node to the scene tree. Click on this node, and in the Inspector, open the &lt;strong&gt;Pathfinding&lt;/strong&gt; section. Make sure that in &lt;code&gt;Navigation Layers&lt;/code&gt; layer &lt;code&gt;2&lt;/code&gt; is selected to match the nav layer of the &lt;code&gt;Sidewalks&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FGPXemKE.png" class="article-body-image-wrapper"&gt;&lt;img alt="the NavigationAgent2D in the scene tree" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FGPXemKE.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FI4xD9VI.png" class="article-body-image-wrapper"&gt;&lt;img alt="the navigation layers in the inspector" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FI4xD9VI.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we will be writing a new script using the agent for pedestrians, remove the existing &lt;code&gt;Movable.gd&lt;/code&gt; script from the &lt;code&gt;Pedestrian&lt;/code&gt; scene by right clicking the root node, and selecting &lt;strong&gt;Detach Script&lt;/strong&gt; from the menu. Now, let's attach a new script to the &lt;code&gt;Pedestrian&lt;/code&gt; and call it &lt;code&gt;MovableWithAgent.md&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We still want some of the same properties that &lt;code&gt;Cars&lt;/code&gt; have, so start by copying over the following class variables from the &lt;code&gt;Movable.gd&lt;/code&gt; script -- we'll need: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;goal&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;goal_pos&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;current_speed&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;arrival_tolerance&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, we'll implement the basic movement using the agent with the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gdscript"&gt;&lt;code&gt;&lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;KinematicBody2D&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;goal&lt;/span&gt;
&lt;span class="k"&gt;onready&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;goal_pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;global_position&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;current_speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;arrival_tolerance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_ready&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# Tell the agent where it should navigate to&lt;/span&gt;
    &lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="n"&gt;NavigationAgent2D&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_target_location&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal_pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_physics_process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Similar to the car, when the pedestrian gets close enough to the goal, despawn it&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;global_position&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal_pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;arrival_tolerance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;queue_free&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# Find the next point in the path to navigate to and update the internal path within the agent&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;next_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="n"&gt;NavigationAgent2D&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_next_location&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# Calculate the direction and movement just as before&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_location&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;global_position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;current_speed&lt;/span&gt;
    &lt;span class="n"&gt;move_and_collide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's talk about our current code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As soon as the &lt;code&gt;NavigationAgent2D&lt;/code&gt; enters the scene tree we need to tell the agent where the &lt;code&gt;KinematicBody2D&lt;/code&gt; should go by calling &lt;code&gt;set_target_location()&lt;/code&gt; in the &lt;code&gt;_ready()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;As the node moves, &lt;code&gt;NavigationAgent2D&lt;/code&gt; keeps track of the path and what the next point is. In order to update the path, we must call &lt;code&gt;get_next_location()&lt;/code&gt; in every physics frame -- hence calling it in &lt;code&gt;_physics_process()&lt;/code&gt; -- which both returns the next location in the path, and updates the array of points forming the path. 

&lt;ul&gt;
&lt;li&gt;Note how this is a simpler version of the code in &lt;code&gt;Movable.gd&lt;/code&gt; that iterates through the entire array of points in the path returned by the Navigation Server. We've now offloaded keeping track of the path to the &lt;code&gt;NavigationAgent2D&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;NavigationAgent2D&lt;/code&gt; is a helper node that builds on &lt;code&gt;Navigation2DServer&lt;/code&gt;. Right now, we essentially have the same functionality as we did using the &lt;code&gt;Navigation2DServer&lt;/code&gt; alone, but optimized. In order to see the similarities, try this: checkout the code at the beginning of this section and modify this line of code -- &lt;code&gt;Navigation2DServer.map_get_path(get_world_2d().navigation_map, global_position, goal_pos, true, nav_layer)&lt;/code&gt; -- setting the fourth argument (the &lt;code&gt;optimize&lt;/code&gt; flag) to &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Great, let's keep going! Set the &lt;code&gt;Goal&lt;/code&gt; property in the 2D Scene Editor for the &lt;code&gt;Pedestrian&lt;/code&gt; as &lt;code&gt;%PedestrianGoal&lt;/code&gt;, just as it was before with the other script. Let's run the script and see what happens.&lt;/p&gt;

&lt;p&gt;As you can see, the pedestrians are moving but still piling up behind the slow walkers. This is because we need to do one final step in order to safely avoid collisions along the path. &lt;/p&gt;

&lt;p&gt;Let's change a couple of things in our scene and script. &lt;/p&gt;

&lt;p&gt;Click on the &lt;code&gt;NavigationAgent2D&lt;/code&gt; node in the &lt;code&gt;Pedestrian&lt;/code&gt; scene tree, and go to the Inspector. Under &lt;strong&gt;NavigationAgent2D&lt;/strong&gt;, find the &lt;strong&gt;Avoidance&lt;/strong&gt; section and turn on &lt;code&gt;Avoidance Enabled&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also within the &lt;strong&gt;Avoidance&lt;/strong&gt; section, set the &lt;code&gt;Radius&lt;/code&gt; to &lt;code&gt;8&lt;/code&gt;, the &lt;code&gt;Neighbor Dist&lt;/code&gt; to &lt;code&gt;200&lt;/code&gt;, and the &lt;code&gt;Time Horizon&lt;/code&gt; to &lt;code&gt;75&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, replace the call to &lt;code&gt;move_and_collide()&lt;/code&gt; on the last line of &lt;code&gt;_physics_process()&lt;/code&gt; with the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$NavigationAgent2D.set_velocity(movement * delta)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is where the magic of &lt;code&gt;NavigationAgent2D&lt;/code&gt; starts to shine. Calling &lt;code&gt;set_velocity&lt;/code&gt; internally calls Godot's collision avoidance algorithm that adjusts the velocity of the node as it moves around obstacles. We didn't even have to think about how to make the people smoothly walk around each other -- Godot will handle it for us! Super cool.&lt;/p&gt;

&lt;p&gt;Since the call to the collision avoidance algorithm is actually a callback, a &lt;a href="https://docs.godotengine.org/en/3.5/getting_started/step_by_step/signals.html" rel="noopener noreferrer"&gt;Signal&lt;/a&gt; called &lt;code&gt;velocity_computed&lt;/code&gt; is emitted once the calculation is done and the node can safely move. This signal also emits the result of the calculation called &lt;code&gt;safe_velocity&lt;/code&gt;. Let's connect this signal to the script.&lt;/p&gt;

&lt;p&gt;Go back to the &lt;code&gt;Pedestrian&lt;/code&gt; scene and click on the &lt;code&gt;NavigationAgent2D&lt;/code&gt; node in the scene tree. In the &lt;strong&gt;Node&lt;/strong&gt; menu, under &lt;strong&gt;Signals&lt;/strong&gt;, find the signal called &lt;code&gt;velocity_computed&lt;/code&gt; in the &lt;strong&gt;NavigationAgent2D&lt;/strong&gt; section. Connect it to the &lt;code&gt;Pedestrian&lt;/code&gt; script.&lt;/p&gt;

&lt;p&gt;Now, we can simply call &lt;code&gt;move_and_collide()&lt;/code&gt; within the receiver method, using the &lt;code&gt;safe_velocity&lt;/code&gt; calculated by Godot. Our final code should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gdscript"&gt;&lt;code&gt;&lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;KinematicBody2D&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;goal&lt;/span&gt;
&lt;span class="k"&gt;onready&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;goal_pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;global_position&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;current_speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;arrival_tolerance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_ready&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# Tell the agent where it should navigate to&lt;/span&gt;
    &lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="n"&gt;NavigationAgent2D&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_target_location&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal_pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_physics_process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Similar to the car, when the pedestrian gets close enough to the goal, free it&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;global_position&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goal_pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;arrival_tolerance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;queue_free&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# Find the next point in the path to navigate to and update the internal path within the agent&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;next_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="n"&gt;NavigationAgent2D&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_next_location&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# Calculate the direction and movement just as before&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_location&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;global_position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;current_speed&lt;/span&gt;
    &lt;span class="c1"&gt;# Sends the movement input to the internal collision avoidance algorithm and sets the agent's velocity&lt;/span&gt;
    &lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="n"&gt;NavigationAgent2D&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_velocity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_on_NavigationAgent2D_velocity_computed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;safe_velocity&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;move_and_collide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;safe_velocity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome, we're all done! Let's run our game and see the results.&lt;/p&gt;

&lt;p&gt;Some final notes about the &lt;code&gt;NavigationAgent2D&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collision avoidance is not actually checking the collision layers of the &lt;code&gt;KinematicBody2D&lt;/code&gt;. Instead, each &lt;code&gt;NavigationAgent2D&lt;/code&gt; registers itself as an agent of a certain size with the &lt;code&gt;Navigation2DServer&lt;/code&gt;, and it can see all other registered agents. &lt;/li&gt;
&lt;li&gt;The size that the &lt;code&gt;NavigationAgent2D&lt;/code&gt; registers itself with is its &lt;code&gt;radius&lt;/code&gt; property (found in the &lt;strong&gt;Inspector&lt;/strong&gt; under the &lt;strong&gt;Avoidance&lt;/strong&gt; section). &lt;/li&gt;
&lt;li&gt;The other properties in the &lt;strong&gt;Avoidance&lt;/strong&gt; section relate to how the agent navigates around other agents. Before the tutorial, we tinkered with the values quite a bit before finding the right mix for our pedestrians. Take some time to play with the values on your own and see how it affects the end result!&lt;/li&gt;
&lt;li&gt;If a future project of yours has avoidable nodes that are dynamic but &lt;strong&gt;should not&lt;/strong&gt; themselves move to avoid others, consider adding a &lt;a href="https://docs.godotengine.org/en/stable/classes/class_navigationobstacle2d.html" rel="noopener noreferrer"&gt;&lt;code&gt;NavigationObstacle2D&lt;/code&gt;&lt;/a&gt; as a child. For static obstacles, it is better to update the navigation map itself. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we learned about Godot's completely new navigation system. They've implemented a system that is flexible and easy to use, especially for those new to game development. Furthermore, they've even given us a built-in way to handle static and dynamic collision avoidance using Navigation Agents, which is extremely powerful. &lt;/p&gt;

&lt;p&gt;We hope you've enjoyed this tutorial and gained a good understanding of how to use Godot's new Navigation System and its advantages. We hope you use this knowledge to go forth and create some awesome new games -- please share them below and showcase your work!&lt;/p&gt;

&lt;p&gt;Follow this blog and our YouTube channel for even more tutorials. Let us know what other topics you'd like us to cover!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
