This is the fourth part of the on going series on how to build a 2D Platformer Game using Godot Game Engine. If you miss the last part you can read it here Godot 3.2 Guide To Build Your 2D Platformer Game – Part 3 (Spawning Platforms Dynamically)
In the Scenes folder add a new folder called Players. Then add a new scene to Players folder and call it Player.
Click the Other Node
Search for KinematicBody2D then click Create
Rename the KinematicBody2D to Player. Right click to the Player to add child node. Search for AnimatedSprite then click Create
Click the AnimatedSprites then click the Frames empty input in the right side of the window.
Click the New SpriteFrames
Click to Frames input once again.
Then a new panel will appear at the bottom
double click to default to rename it. Rename it to idle. Then in the assets folder look for bunny2_ready.png then drag it to the part that I encircle.
You should now have this
Click the paper icon with a plus.
It will add a new animation. Rename it to walk.
Select walk, then drag this two images to animation panel.
Change the speed to 10 and go to right side panel. Check the Playing On
Add another animation and name it jump. Drag the ready and jump image to animation panel. Then turned off the loop.
Add a child node to the player. Select CollisitonShape2D then click Create.
Select the AnimatedSprite then set the animation to idle
Now Select the CollisionShape2D then click on the empty input for the shape.
Then you will have this.
Drag the red dots to cover the sprite, similar to this
Add another child node to Player. Search for AudioStreamPlater2D then click Create
Rename it to JumpSound. Then drag the Audio that we downloaded it Part 1 to the Stream input.
Click the Player node then click the add script icon.
Click the folder icon and navigate to Scripts folder.
Then click Open
Update you Player.gd code to this
Or copy and paste this code
extends KinematicBody2D const SPEED = 250 const JUMPFORCE = 1000 const GRAVITY = 50 var velocity = Vector2(0,0) onready var jump_sound = $JumpSound onready var animation = $AnimatedSprite func _physics_process(delta): if Input.is_action_pressed("ui_right"): velocity.x = SPEED if is_on_floor(): animation.play("walk") animation.flip_h = false elif Input.is_action_pressed("ui_left"): velocity.x = -SPEED if is_on_floor(): animation.play("walk") animation.flip_h = true else: if is_on_floor(): animation.play("idle") if is_on_floor(): velocity.y = 0 if Input.is_action_just_pressed("ui_up") and is_on_floor(): velocity.y -= JUMPFORCE jump_sound.play() animation.play("jump") velocity.y += GRAVITY move_and_slide(velocity, Vector2.UP) velocity.x = lerp(velocity.x, 0, 0.2)
To explain the code, we declare a speed and jump force. These variables will be use to moving and jumping. The gravity is to pull the player down after jumping or whenever the player is not on ground.
If you are wondering where I get the inputs that I checked in the if statement. You can go to Project Menu then Project Settings. Look for the Input Map tab. You can see the inputs there, you can also add you own inputs if you want.
Now time to test our game. Go to game scene. Click Game node then click chain icon.
Select Player.tscn then click Open
Now you should have this. Click Play button.
Our player is to big for the platforms. Let’s scale the player
Go to Player scene. Click the Player node then go to transform section at the right side of the window. Then change the scale to 0.4 for x and y.
Then go to Game scene. Select Player node update the position for x and y to 100.
Play the game again and you should have something like this.