Godot 3.2 Guide To Build Your 2D Platformer Game – Part 5 (Camera That Follows The Player, Background And Resetting the Game)

This is the fifth part of the 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 4 (Creating the Player)

Advertisements

Go to Player scene and add child node. Select Camera2D then click Create

Click the Camera2D then go to inspector panel. Click the Current On, Drag Margin H and Drag Margin V. Set the Left limit to 0, top limit to 0 and bottom limit to 500. Then Change the Drag Margin right to 0.

Now Play the game and you will notice that the camera is following the player now.

Advertisements

We have one more problem, if the player falls the game is still continuing but we can see the player. To resolve this add child node to Player. Select the VisibilityNotifier2D then click Create.

Click the VisibilityNotifier2D then click the Node tab at the right side of the window. Then double click on screen_exited(). When a popup window appears, click Connect.

It should add this code at the bottom of Player.gd

Change it to this

With that change, this will be the entire code of Player.gd

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)


func _on_VisibilityNotifier2D_screen_exited():
	get_tree().reload_current_scene()

Try the game again. Now when the user falls, it will reset the game.

Advertisements

Now, let’s add some background to finish this tutorial.

Right click to the Scenes folder and add a new folder called Backgrounds. Then add new scene in the Backgrounds folder and call it Background.

Click 2D Scene

Rename the 2D Node to Background.

Add child node to Background. Select ParallaxBackground then click Create

Right click to ParallaxBackground then add child node. Select ParallaxLayer then click Create

Right click to ParallaxLayer and add child node. Select Sprite then click Create.

Select the sprite then drag the bg_layer1,png to sprite texture

Select the ParallaxBackground then go to transform. Set the offset x to 510 and offset y to 300. Then Scale x and y to 0.55

Select the ParallaxLayer then set the Mirroring x and y to 1.

Select ParallaxLayer then press Ctrl + D or just right click then select duplicate.

Set sprite texture for the ParallaxLayer2 to bg_layer2.png

Duplicate ParallaxLayer2, it should create a ParallaxLayer3. Set the sprite texture for the ParallaxLayer3 to bg_layer3.png. Then Duplicate the ParallaxLayer3 to create ParallaxLayer4 and set the sprite texture to bg_layer4.png. You should now have this

Click the ParallaxLayer4 then change the Mirroring x to 1500

Advertisements

Go to Game scene. Click the Game node then click the chain icon.

Select Background.tscn then click Open

Run the game again and you should now have this game

That’s it for this series. I will create another one when I got a free time. A continuation of this one, on how to add game menu.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s