How To Create Player On Platform With Godot (Free)

How To Create Player On Platform With Godot Free

Godot is a good and free open-source game engine that can be used for personal and commercial projects of any purpose, not just games. To illustrate the power of this game engine, let us build a simple game environment for a player that can navigate around on a platform.

The goal here is just to create a player or sprite that can move left or right and jump around a platform environment. It is not a complete game but this tutorial guide may serve as a taste of what working with the Godot game engine can be like.

Readers should at least learn about the basics of the Godot game engine interface. Feel free to use this post to refer back and review Godot interface basics.

Godot nodes
Godot nodes

Just download and double-click to run the Godot game engine file.

Start a new project with a new empty folder.

Godot start screen
Godot start screen
Select 2D from above
Select 2D from above

Click on 2D Scene

Nodes overview on the left
Nodes overview on the left
Click on Node2D
Click on Node2D to rename it e.g. MainScene

Notice the Inspector on the right where we can change the options e.g Transform and visibility

Inspector
Inspector

Type CMD+S (or Ctrl+s for Windows) to save the scene e.g. MainScene.tscn.

Save the scene
Save the scene

The filesystem is where we can have an overview and start importing assets into the game.

Overview of all our assets
FileSystem for an overview of all our assets

We can also click 3D from the top to view it in 3D. Next to it is the Script part where we put in the codes for the nodes or assets.

3D view option above
The 3D view option above

The middle or right mouse click can move the axis point around.

The middle or right mouse click can move the axis point
The middle or right mouse click can move the axis point

On the top-right are the buttons to play, pause, and stop the game. We can also play the scene with the action-cut icon. The last is the custom scene for selecting a scene to launch.

Play and stop game
Play and stop the game and the scene

Let us start importing some assets into the game.

Right click res and select File Manager
Right-click res and select File Manager
Create audio font and sprite folders
Create audio font and sprite folders

Start gathering some assets for your game e.g free assets from Kenney.nl. And import these assets into your asset folders,

Kenney 1
Kenney

For example, we need to collect a player sprite (idle, jump, first walking, second walking poses), a robot sprite, a coin or reward sprite, a sound for collecting the reward, and font or two for the game. For the purpose of this simple tutorial, just get one sprite image and one tile.

All can be found on Kenney for free.

Imported assets in Godot game
Imported assets in the Godot game
Double click on the asset to view or modify in the Inspector on the right.
Double click on the asset to view or modify it from the Inspector on the right.

Next, we will create a player in a new scene.

Create a new Scene
Create a new Scene

For a player sprite, we need to apply physics to it. Click on Other Node -> Kinematics2DBody.

Other Node and KinematicBody2D to apply physics to the sprite
Other Node and KinematicBody2D to apply physics to the sprite

We will leave the Inspector physics settings for this Kinematics2DBody alone for now.

Built in physics settings for Kinematics2dBody node
Built-in physics settings for Kinematics2dBody node

We may double click the word Kinematics2dBody to rename it e.g. Player

Double click Kinematics2dBody to rename it
Double click Kinematics2dBody to rename it
WIth the Player scene selected drag a player sprite image into the main space
With the Player scene selected drag a player sprite image into the main space

Whatever happens to the parent Player node will apply to all the children nodes.

This visual node is a child of the Player node
This visual node is a child of the Player node

Move the sprite image to the center of the axis
Move the sprite image to the center of the axis
Set the transform position to 0 for x and y in the Inspector
Set the transform position to 0 for x and y in the Inspector
Save the scene with the name Player
Save the scene with the name Player
Rename PlayerIdle to Sprite
Rename PlayerIdle to Sprite

Next, we need to fix the collide node error message. Right now it does not know how it can collide with other objects without a shape.

Collide node warning error to fix
Collide node warning error to fix
Add a child node with right click or commanda
Add a child node with right-click or command+a
Search and create a CollisionShape2D node
Search and create a CollisionShape2D node
With the CollisionShape2D selected go to Shape in Inspector and select NewRectangleShape2D
With the CollisionShape2D selected, go to Shape in Inspector and select NewRectangleShape2D
Collider square area is too small for the sprite
The collision square area is too small for the sprite
Adjusted collision area for the sprite
Adjusted collision area for the sprite
Put the Player into the main scene with click and drag
Put the Player into the main scene with click and drag

A Player node that is an instance of the Main Scene appears.

Player node instance
Player node instance

Any changes made on the Player scene will also update on the Player node in the Main Scene

Zoom out and postion the sprite anywhere in the blue rectangle
Zoom out (top-left corner for zoom controls) and position the sprite anywhere in the blue rectangle
Click Play and sprite will appear
Click Play and sprite will appear

Quit the game with the stop button or close the game window. Congratulations, now you have managed to display a sprite on our Godot game.

Next, we will begin to control the movements of our sprite with scripting.

Project Project Settings
Project Project Settings
INput Map for project settings
Input Map for project settings

We are interested in 3 inputs: left, right, and jump. Let’s create a new one by typing in the Action box and clicking Add: move_left

Add key for an action
Add key for an action
Press left arrow key and click Ok
Press the left arrow key and click Ok

Repeat by creating move_right for the right arrow and jump for the up arrow.

Create a new script for the Player node
Create a new script for the Player node
Click Create to attach node script
Click Create to attach node script
Player GDScript
Player GDScript

Delete everything except the first line so we can use functions the KinematicBody2D such as move, slide, and collide.

Delete everything except the first extends line
Delete everything except the first line

Next, we create different variable types and assign some values.

Create GDScript variables for the game
Create GDScript variables for the game

We also create a Sprite date type for our sprite so we can modify values dynamically during the game. For example, we want the sprite to face left when moving left and right when moving right by setting the Flip H (horizontal) value of the Sprite.

Flip H for the sprite image
Flip H for the sprite image

We get an error below the code screen that prompts us to use another function. Now the error is gone. We get the sprite only after it is initialized or ready with the ‘onready’ command.

Modify sprite code line
Modify sprite code line

Next, we will make the sprite move by calling the _physics_process built-in function.

Code the basic physics of the sprite, save and run the game.

Basic physics code for the sprite
Basic physics code for the sprite
Sprite can now move left and right with the arrow key
Sprite can now move left and right with the arrow key

Next, we make it face the direction of the movement.

Flip the image moving in the direction of movement
Flip the image moving in the direction of movement

Save and click play to check.

Sprite was flipped moving left
Sprite was flipped moving left

Next is the application the gravity and the ability to jump.

Apply gravity and jump physics
Apply gravity and jump physics

Note: I amended the variable typo from jumpforace to jumpforce.

Save and click play. We have not applied a ground for the sprite to stand on, so it will fall right down once the game is started.

Falling sprite
Falling sprite

Next, we will create the tiles for the game.

Create a new scene. Click + Other Node. Select StaticBody2D for creation.

Create StaticBody2D for our new scene
Create StaticBody2D for our new scene

This type of node is static and useful for collision.

Rename to Tile
Rename to Tile
Save scene as Tile
Save scene as Tile
Drag the tile image into the Tile area and rename it as Sprite on the left
Drag the tile image into the Tile area and rename it as Sprite on the left
Add a CollisionShape2D child node
Add a CollisionShape2D child node
Set the shape in Inspector for CollisionShape2D to be RectangleShape2D
Set the shape in Inspector for CollisionShape2D to be RectangleShape2D

Position both the tile and rectangle collider area to 0 on the x and y-axis with Transform.

Go to MainScene and click and drag Tile TSCN file onto the MainScene screen area
Go to MainScene and click and drag the Tile TSCN file onto the MainScene screen area

Save and click play.

Sprite now stands on the tile
Sprite now stands on the tile
Sprite can jump as well
Sprite can jump as well

Sprite will fall if it moves beyond the tile floor.

Now, we can build a platform by duplicating the tile with Ctrl+d or CMD+d and dragging it out with a mouse click.

Select the tile or titles and press CMD and D to duplicate and drag it out
Select the tile or titles and press CMD and D to duplicate and drag it out
Use Grid Snap to easily align the tiles straight
Use Grid Snap to easily align the tiles straight
Tlle platform built with duplication
Tile platform built with duplication
Player can navigate on this simple platform built with Godot
The player can navigate on this simple platform built with Godot

At this point, the player or sprite can move around this little one-scene platform.

What next?

This is far from being a complete game e.g. player can vanish moving beyond the screen, there are no opening welcome screen or ending credits, or lacks any interaction with other objects or nodes. Game development is a long and hard journey, at least for a decent release. And given the extreme high and tough competition out there on various platforms, success is an anomaly.

Conclusion

Godot is a free, lightweight, and stable game engine for creating games. However, building a game will require some learning, and access to support may be important if we get stuck. Another reason I recommend Godot is that they are known for their friendly and nice community.

The hardest part of this tutorial or games development to understand may be the programming physics logic. The version of the Godot used here is v3.4.4 stable. The game engine and all assets used are all released for free use.

One of the problems I realized with learning from free resources maybe they are incomplete on purpose e.g. building a complete game. Rather than learning from one free resource, and then scouting to see if we can find the next level resource, or not. This is not ideal for an arduous project like game development, and definitely without support when we run into problems that will happen.

We can learn from free game development YouTube videos and blogs, however, a more complete and effectively faster learning path is to follow a good and most likely a paid course as well. Our human time and energy are too limited, even free learning resources do not mean it will be worth it.

I believe one of the more complete and comprehensive Godot resources with both the Godot and GDQuest course and community support can be GDQuest’s Learn to Code From Zero With Godot (good for beginners).

Be wholesome.

More Information

What are some Godot or game engine-related posts?

Good Passive Logo

Sign up to receive Good Passive content in your inbox.

We don’t spam! Read our privacy policy for more info.

Leave a Reply

x  Powerful Protection for WordPress, from Shield Security
This Site Is Protected By
Shield Security