Let’s Learn Godot 4 by Making an RPG — Part 4: Game TileMap & Camera Setup🤠
Tags: tutorial, beginners, gamedev, learning
Published: 2023-06-22 07:42:06

Welcome back to our Dusty Trails tutorial series! If you missed the previous part, you can find the links to it at the end of this tutorial.

We now have a functioning player, well, sort of, we still have a lot to add to our little person to make them a real game hero! But for now, our player has to wait, because in this part we are going to add our games [TileMap](https://docs.godotengine.org/en/stable/tutorials/2d/using_tilemaps.html) with collisions, as well as add a [Camera2D](https://docs.godotengine.org/en/stable/classes/class_camera2d.html) to our game so that we can follow our player around. We didn’t add the camera node during the player setup section of this tutorial because the camera node does not work without some sort of background, which is our tilemap.

---

>  ***WHAT YOU WILL LEARN IN THIS PART:***
>  · How to work with sprite sheets for the TileMap node.
>  · How to work with the TileMap and TileSheet panels.
>  · How to use the TileMap tools to paint tiles onto the map.
>  · How to create tilemap layers, as well as physics and terrain layers.
>  · How to add a camera to your player.

---

First things first, what is a tilemap or tileset? Well, tilemaps are the base for building a game world or level map out of small, regular-shaped images called tiles. These tiles are stored in something we call an atlas, tileset, or spritesheet, which we’ve seen before when adding our animations. In other words, the tileset is the actual graphics of the tilemap.

---

>  ***What is the difference between a TileMap and a TileSet?***
>  The TileMap is the grid where you place tiles from the Tileset to actually build your level. A Tileset is a collection of tiles that you can use to build your level.

---

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*PHy7W2i40sJYWhl5DSJhng.png)

![Figure 1: TileSet (left) and a TileMap (right)](https://cdn-images-1.medium.com/max/2000/1*-QRgukv0E3x9zyun564vIA.png)
> Figure 1: TileSet (left) and a TileMap (right)

In our Main scene, we can add a new node of type TileMap. Click and drag this node so that it is behind our Player scene, otherwise, the player will be hidden by our map!

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2920/1*EeDaQ_7pZF5fSIifjbU9Zw.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*nvahGHwdjHmhlStTCoMzRg.png)

Let’s rename this node as Map, and in the Inspector panel, click on <empty> next to TileSet to create a new tileset.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/3840/1*TEG-TGwmN7ZXWJ5TU39Ccw.png)

You will see two new options pop up below: [TileMap ](https://docs.godotengine.org/en/stable/tutorials/2d/using_tilemaps.html)and [TileSet](https://docs.godotengine.org/en/stable/tutorials/2d/using_tilesets.html).

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2254/1*DQ9PkrFzzLqfE6H5F4iMXQ.png)

To create our TileMap (which is the map we will draw on our screen), we first need to assign it a TileSet (which is the image we will draw from). To do this, click on the TileSet option, and open your Assets folder in your FileSystem panel. You will see that I gave you three tilesets: Interior, Water, and Atlas. We will use these tilesets to create our world.

You can drag these tilesheet into the Tiles option in your TileSet panel.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2952/1*08WUhNUEaLYPcwJHv4vtwQ.png)

A popup will arise asking you if you want the engine to create automatic tiles. Say yes to this prompt, because this will break our image into nice squares to form a grid.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*WA1F9FX63XcQsPRvm_kdZw.png)

You can now change the tilesheets name, ID, and grid separation values. Change your names and ID’s as follows:

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2722/1*e3m1PU1YTPwrPOyMlliCGw.png)

Now if we go back to the TileMap panel, we can see that our tiles have been added. You can now click on the tilesheet and start painting the images onto the screen.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2724/1*I9tg9JdUrEfgDdcqiCr2GQ.png)

**Here is a brief overview of the tools present in the TileMaps panel:**

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*U2H97tgJuDppRWgxdoZP9g.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/3840/1*WyoRB5JwLpt9s0S2T3C85Q.png)

The next part will be a bit confusing, so I’m going to try and break it down into sections so that you can better understand the concepts. If things get a bit too complex for you, please [download](https://github.com/christinec-dev/DustyTrails_RPG_Parts/tree/main/Map) the Map scene that I created and drag in the Map node from the scene into your Main scene.

## **Map Creation**

### **1.** **Terrain Layers Introduction (Auto-Tiling)**

We can paint or create our map with just the paint tools provided, but our environment would look pretty bland, especially for our terrain. To make things easier for us, we can make use of something called [autotiles](https://docs.godotengine.org/en/stable/tutorials/2d/using_tilesets.html).

Terrains — also referred to as autotiles, create organic terrain shapes that will autonomously draw corners and edges in the area that we tell them to. This removes the tediousness of manually placing down edges and sides of say grass patches or water bodies while trying to create interesting-looking terrains ourselves.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*NL-NB1inyVs9klWVTO2rlg.png)

To create autotiles, simply go over to the Inspector panel, click on the TileSet resource (the one with the image), and underneath Terrain Sets add a terrain set element. Remember to add an element to the terrain set element as well.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*pLHzqg7akE7w5Pc5cSjtfA.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*t1E7pjWwALskty1G45dFRA.png)

You can create different terrain sets for different terrain elements. For example, you’ll put your grass autotile on one set, and your dirt autotile on another set, and so forth.

Let’s create terrain elements for grass, sand, and water. You will see from the image below, that I have a Sand_Dessert and a Sand_Forest. That is just to show you that you can have multiple sands in one terrain element set. Grass, sand, and water, in my case, are all in different sets, but the sand elements are in the same set.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*Mznl8KBDj6Bn6wOGLXfQKw.png)

### **2.** **TileMap Layers**

While we’re at it, let’s also create [layers ](https://docs.godotengine.org/en/stable/tutorials/2d/using_tilesets.html)so that we can paint these elements on separate layers so that they can overlap. If we paint on the same layer, the tiles will erase each other.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*ytycEJKikMhzz3kkJPHRMw.png)

To add a new layer, simply go into the Inspector panel, and underneath the Layers property, add a new layer element. Let’s add one for each of our terrain sets: water, grass, and sand.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*PgZfh9OAHorpQR5Shltslw.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2246/1*dXF-9nLZBfuFwL2d8EkH-g.png)

### **3.** **Terrain Layer Creation (Auto-Tiling)**

We can now paint on our layers separately. But first, let’s create those autotiles. In your Tileset panel, on top, you’ll see three options: setup, select, and paint.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*5iUmowLqZptK5wK_G2vEqA.png)

In setup, we’ve already seen that it is here where we can change the name and ID of our tileset. In our select panel, after we’ve added layers such as our terrain or physics layers from the TileMap properties, we can change the properties of tiles, i.e., add collisions or autotiling to groups of tiles via selecting them and then changing them. In the paint panel, we can do the same as in the select panel, but this time we can just paint these properties onto our tiles, i.e., painting collisions to multiple tiles at once.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*yEB2McWr6Kfm3Tt1OMhK-w.png)
> Figure 8: TileSet Modes overview

**a. Autotiling in the Select Panel**

Remember, the select panel only allows us to select individual or multiple tiles and then change their properties via the properties panel.

You’ll see that our Terrains property has two numerical values: -1 for the Terrain set, and -1 for Terrain. This refers to the ID of our terrain sets and terrain elements. -1 means “no terrain set” or “no terrain” (it will erase the terrain), which means you must set Terrain Set to 0 or greater before you can set Terrain to 0 or greater.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*dl3AzsHpLK-5HqFLqTyXFw.png)

For our terrains, we have three terrain sets, one for sand, water, and grass. In each of those terrain sets, we have terrain elements. For our sand terrain set, we have the Sand_Dessert terrain element and the Sand_Forest terrain element. For our water terrain set, we only have the water terrain element. And for our grass terrain set, we only have the grass terrain element. I hope this is making sense because it might be a bit confusing or overwhelming.

**For our terrain layers, our elements will correlate as follows:**

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*rZY63GibvwC_6uWT8-wnfw.png)

Let’s add autotiles to terrain set 0 (sand), terrain element 0 (Sand_Dessert). We’re going to use our Atlas.png tileset for this. Now we can go ahead and select our sand tiles. I’m going to select the top-left tile.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*j4_1MkEibiIRWRfOd7GbZA.png)

Now, we’ll have to assign the bit-peering values for these tiles. The peering bits determine which tile will be placed depending on neighboring tiles. -1 is a special value that refers to empty space.

For example, if a tile has all its bits set to 0 or greater, it will only appear if all its neighboring tiles are using a tile with the same terrain ID. If a tile has its bits set to 0 or greater, but the top-left, top, and top-right bits are set to -1, it will only appear if there is empty space on top of it (including diagonally).

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*hyJLF_zq6H9o7KhVXYGESQ.png)

When we assign this value, we want to avoid assigning values to obvious corners or sharp edges.

If we go back to our TileMaps panel, underneath our Sand_Dessert terrain element, we can now paint our newly created autotile onto the map. Now this won’t do anything because we need to add the other sand tiles. For this, we will speed it up via the paint panel, where we can paint the bit peerings directly onto the terrain.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2258/1*BGgRgkpS-svknp0I2T3REA.png)

**b.** **Autotiling in the Paint Panel**

In your TileSet panel, underneath paint, you can see that you can select your terrain sets and elements directly, without providing its ID.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*f465NoZebZNG0UjBHno5cQ.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*dzoAoCiEIrK614BsRzAPxg.png)

Let's make sure that our terrain set element for Sand_Dessert is selected and, on your tiles, start drawing in the bit peerings directly. Remember to avoid corners, sharp edges, or lines. To add a bit value, left click on your mouse on the tile. If you need to erase a bit value, just right-click on your added bit value to remove it.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*txXR5Yluru3cUNRpNAGJdg.png)

Now if you go back into your TileMap panel, you will see your other tiles added to your terrain set autotile. If you select your Sand_Dessert terrain and start drawing on your map, you'll see that it automatically draws nice, organic terrains.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2256/1*QoMhGONGSPhMPwzKni5OGA.png)

We can now go ahead and create autotiles for the rest of our terrain set elements. If you cannot see your bit blocks, you can always change the terrain element color in the Inspector panel underneath Terrain Sets > Element > Color.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/3140/1*qayuDzIii-3hWvFd4wx_Fg.png)

*This is the terrain bits for our Sand_Forest autotille (using Water.png tileset):*

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2246/1*3mwI7Y9CZ-ZXMWZFIUia4w.png)

*This is the terrain bits for our Grass autotille (using Atlas.png tileset):*

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2456/1*N0OhxshU1RF62BUEbeGIyA.png)

*This is the terrain bits for our Water autotille (using Water.png tileset):*

![Godot Learning RPG](https://cdn-images-1.medium.com/max/3134/1*TQoJKcwELCKzZDpNR88jeA.png)

### **4.** **Painting Our Terrain**

For our game, I will use my rectangle tool to draw a semi-large area of water using the water autotile. How large you draw this rectangle does not matter, what matters is how many objects you add to it. Too many items in a singular scene will take up a lot of memory. So just keep that in mind.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/3818/1*YG_gVMgRQjDN2zqeiL3KPw.png)

I don't want my player to run over this water, so this will be the bounds of my map, i.e., the player will stop when they run into water. We'll add the collisions for this water in a second.

Next, on our grass layer, I'm going to draw the land for my map. Go crazy with this design. Just keep in mind your navigation paths for your map. If you want a better idea of the workflow behind designing maps, go and look at [this article.](https://www.worldofleveldesign.com/categories/csgo-tutorials/csgo-how-to-draw-top-down-layouts.php) I'm just creating this map for tutorial's sake, so whatever I end up with does not matter since I will not be selling or releasing this game!

![Figure 2: Example of a map layout](https://cdn-images-1.medium.com/max/3840/1*FWBwJRMbsqpaOnmaTRwxlQ.png)
> Figure 2: Example of a map layout

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2454/1*LzpFCZ6eb4bn43kMbCjh5A.png)

We also will draw the collisions in for the edges of the grass tiles later on. Let's finally add our sand to our sand layer. I'll only be adding a few pieces of the Sand_Forest tiles since the grass edges don't match our main grass. I just created it to show you how the sets work.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2464/1*OUwmDPG6041nnt2PzRrJ9w.png)

You can add the grass from the Water.png tilesheet if you want to create different environments/sections for your maps.

### **5.** **Adding World Items**

Now that we have our terrain set down (remember, you can always make changes to it later on), we can go ahead and add things like flowers, trees, and even buildings. Before we do this though, let's add collisions to our tiles so that our player can run into items.

For this, we need to add physics layers to our TileMap. In your Inspector Panel, make sure your tileset resource is selected, and underneath Physics Layers add a new element.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*hofnVqshyjX6dbvIEPX9kQ.png)

After creating a physics layer, you will now have access to the Physics Layer option in the TileSet panel.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2474/1*iMI2L-NEh4LAb6aUFI8mHQ.png)

We can now add collisions by either adding them in the select panel or the paint panel. Let's do it again both ways.

**a.** **Collisions in the Select Panel**

In our select panel, let's add some collisions to our tree in our Atlas.png tilesheet. We don't want our player to be able to run through this tree at all, so select the bottom left root of the tree, and underneath Physics Layer > Polygons, we can start drawing in our collisions in that little box. Let's border the entire image outline. You can edit and delete the points with point tools above the box once you've completed your border.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2448/1*_ZPXZ_LfN_CcMHhMmPJHJA.png)

Once you've finished drawing your collisions border or outline, and you've connected your last points, you will see that the shape drawn turns red. This means it now has a collision polygon added to it.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2470/1*9ToJ5C6Lqv8JbdRicfq_4Q.png)

**b.** **Collisions in the Paint Panel**

You can also paint your collisions onto your shape via the paint panel. Select your Physics Layer in your paint panel and click on another tree tile. If you click on it, a box will appear. The blue means that a collision shape has been added to the entire tile, but you can drag in new points to outline the shape you want.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2458/1*Cx8Z3hF0CsyAG68uU0WWOA.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2452/1*2RkFQtuVQEX4uGLpgYmlTg.png)

Now, click on the tile you want to assign the collision shape to - to assign it. You can also paint this collision shape onto multiple tiles if you'd like.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2472/1*fPH4LhqEQ-D2dWeamO1PkQ.png)

I'm not going to do that, so let's go ahead and draw in the collision shapes for the rest of our tree.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2454/1*A9ee7ahndA_dcEC_64omQA.png)

Let's create a new layer and call it foliage.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*vOGGFhkjEQKYhUSFuVEl_Q.png)

Now on this layer, select the tiles that make up the tree, and paint it onto your map (close to your player).

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2454/1*c6JxcCUXmqMFTHjNDSNQmA.png)

You can also go ahead and use your select tool to select the entire tree to create a pattern of the tree so that we don't have to keep on re-selecting all the tiles just to create new trees.

You can just copy and paste or drag this selected item into the Patterns panel in your TileMap option.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2456/1*Gs_ynUMExu5qCA3REz7H5w.png)

Now you can easily paint your tree as a singular object.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2450/1*JtHm6oJPGQvdgS4a-av1qg.png)

If you run your scene, you will now see that your player runs into the trees, and not through them. Let's add collisions to other objects, such as rocks, and our terrain edges.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/3840/1*Qp5lOwRAcI7xoYZ4NiXSWQ.png)

You can go ahead and add collisions to your water and anything else you want to add to your world! Also, don't forget to create patterns for your tiles.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2464/1*KfEnhL6NMVqz3GnbCgXzCg.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2436/1*FiIp7ho-IyusTylqRGBstQ.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2466/1*G8HFC46jRvBqdg_YM575sQ.png)

### **6.** **Create Your Map**

You can also use the scattering tool (the dice icon) to place these tiles randomly on the map. The higher the scattering value, the fewer tiles will be placed in a close radius of each other.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2464/1*d19hOhPxKsTMjpmctRSWLw.png)

I'm going to go ahead and create a basic map with placeholder buildings and such. Really let your creativity flow here.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2450/1*mTpiiU_KCLVvg0TusDAnyA.png)

***Note that if you notice that your player isn't moving, it's because of the water collisions. You can fix this problem by just removing the water tiles from underneath your land/ground.**

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2446/1*jLYI6-dlOy6_WjrV8DcfKw.png)

## **Player Camera**

Okay, now that we have our map, we will notice that if we run our scene, our player will run off of the map and we can't follow them! To fix this issue, we need to add a camera to our player. If you want to capture your entire world, you will attach the camera to your main scene, but if you want to follow a specific character around, you will attach the camera to their scene.

Let's open up our Player scene, and in the Scene Dock, let's add a [Camera2D ](https://docs.godotengine.org/en/stable/classes/class_camera2d.html)node.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/3816/1*21rOqwOt9tUXz_20o7-qEw.png)

You will notice that it creates a purple border around your player. This is the camera frame, and we can add [limits](https://ask.godotengine.org/27004/techniques-for-changing-the-limits-of-camera2d) to these edges so that our player cannot see beyond a certain point in our map. Since we only have water around our map, we do not need to worry about this limit for now.

![Godot Learning RPG](https://cdn-images-1.medium.com/max/3146/1*MuUI5kuIlNoR0QJ3TEzDdw.png)

![Godot Learning RPG](https://cdn-images-1.medium.com/max/2000/1*RihP7lapxxq2SwEsxlY3Ow.png)

And that's it for this part. I know this was a very long section, and it might have even overwhelmed you a bit, but don't worry because in the next part of this tutorial series, we will be doing something new and simple when we start creating the game's GUI. Remember to save your game project, and I'll see you in the next part. 

Your final code for this part should look like [this.](https://github.com/christinec-dev/DustyTrails_RPG_Parts/tree/main/Part_4)

<a href='https://ko-fi.com/J3J2LP2U5' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi2.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

---
### FULL TUTORIAL

![Godot RPG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pd9n6khb9jtaj0bynp7e.png)

The tutorial series has 23 chapters. I’ll be releasing all of the chapters in sectional daily parts over the next couple of weeks.

If you like this series or want to skip the wait and access the offline, full version of the tutorial series, you can support me by buying the offline booklet for just $4 on [Ko-fi!](https://ko-fi.com/s/19c32424e5)😊

You can find the updated list of the tutorial links for all 23 parts in this series [here.](https://oops-i-devd.gitbook.io/oops-i-devd/godot-tutorials/dusty-trails/tutorial)