Setting Up a 2D Scene in Godot
We now know how to create a set of tiles to draw into our 2D game, and can now form a level in which to put a 2D character. But before we do that, we need to make sure that our tiles have colliders - otherwise our character would just fall right through them.
I've created a new Godot project and created a new 2D scene by adding a 2D root node. I've also re-named this node by double-clicking it in the scene menu, so now it's called "Level".
As a child of this first node, I'll create a TileMap node.
I've given the TileMap node a new TileSet in the inspector, taken the tile atlas I made in my last blog post and inserted it into the TileSet (we cover how to do this in my last blog post here).
Adding Collision to Tiles
Now, if you click on one of the tiles, its properties will show up in the menu beside it. Scroll down and you'll come to a section called "Physics". Click on that and you'll see the name of our newly-created Physics Layer. If we made more Physics Layers, they would appear in this list, but right now we only need the one.
At the bottom is a picture of the selected tile, and above it are some polygon-drawing tools. On the picture, the parts that are highlighted in red will be the parts that can be collided with. Right now, none of it's in red, but if we click on the green "Add Polygon" button and use it to connect four points together, it will form a red square on the image, signifying that part will be collided with.
Alternatively, if you want to whole thing to be selected in red, you can press the sideways "..." button and click on "Reset to Default Tile Shape".
To further show what I mean, let's add collision to our slope tile. We can tell from looking at it we don't want the whole thing to be red - otherwise the character won't be touching the grass. We want our red to end where our grass ends. So instead of a square, we'll draw in a triangle and have it fill up the slope while leaving the air above it blank.
Setting Up a 2D Character
A CharacterBody2D is a type of PhysicsBody2D, which is a 2D object affected by physics. However, the CharacterBody2D is actually doesn't have any physics of its own unless you code them into the object itself. Since the player character is going to move around the stage in a very specific way, it's up to you to set those parameters by giving the character their own physics in the code. They do, however, interact with other physics-based objects without having to code them (so they can bump into a wall just fine, but they can't jump over it until you give them code that lets them go up and come down).
When you first insert the CharacterBody2D, a yellow exclamation mark will pop up next to it, saying that it wants to have a CollisionShape2D as a child node (so it has a shape that can bump into other shapes).
So, I'll right-click on the CharacterBody2D and add a child node, and search up "CollisionShape2D" to add to it.
But then another yellow exclamation mark shows up, telling us that we haven't actually picked what shape we want our CollisionShape2D to be.
We can pick our shape over in the inspector on the right-hand side, where it says "<empty>". I'll give it a CapsuleShape2D, since most video game characters tend to have capsule-shaped colliders.
A blue capsule will appear right in the middle of our viewport, showing us where our character will be.
The image is a little big compared to the collision capsule. I'm going to grab the image and shrink it down by shift-dragging its edges so it fits just about inside the capsule.
Adding the Character to the Scene
Now with our player character completed, we can go back into the level scene, click the player scene's name from the bottom-left FileSystem menu and drag it into our level scene.Now let's run the scene to see how it all looks. When you press the play button for the first time, Godot will ask if you want to make the current scene the main scene to run every time we press play. Since this is the main level of our game, we do, so click on "Select Current".
Rather than scaling up everything in the scene, let me go up to Project in the top toolbar, select Project Settings, then click Window (it's under "Display"). In the Stretch segment, next to where it says Scale, I'll increase that number to 4.
If we go back and try playing our game again, we can see it... uh... let me just grab everything and move it up a little with the move tool (press W in the viewport to activate it).
Comments
Post a Comment