Aego Русский

Platformer from scratch

This guide builds a small platformer in an empty 2D project: a character that runs and jumps, a camera that follows it, a pit with a checkpoint, coins and an enemy. Every step ends with the scene file as it looks after the step, and every scene and script on this page is run by the documentation checks.

Start with Your first project if you have not opened the editor yet.

The finished example is a separate, more detailed project at examples/platformer. Open it with aego edit examples/platformer and load its main scene to inspect the game used by the website demo. Move with A/D or the arrow keys, jump with Space, attack with J and open settings with Esc. F6 toggles lighting and F7 cycles its debug views.

This guide starts from simpler shapes rather than reproducing every asset in the finished project. For its lighting, surface maps, particles and post-processing, see Platformer lighting and effects.

On this page

1. Floor and player

Create a project from the 2D template. In the Hierarchy, create an empty node and name it Floor. Give it these components in the Inspector:

Move it to 0, 3. Create a second node, Player, with:

Motion 2D moves a kinematic body: it slides along walls, stands on floors and climbs small steps. Press Play: the player falls and lands on the floor.

2. Run and jump

Create a script Player in scripts/game and attach it to the player:

package game

import "aego/input"

script Player {
	func Update(dt float) {
		m := node.Get<Motion2D>()
		m.Move(input.Axis("Move"))
		if input.Pressed("Jump") {
			m.Jump()
		}
		if input.Released("Jump") {
			m.JumpReleased()
		}
	}
}

Move and Jump are input actions of the 2D template: the arrows or A and D, and Space, or a gamepad. Move takes a value from -1 to 1 and Motion 2D accelerates to that share of its Max Speed. JumpReleased cuts the jump short when the button is let go early, so a tap makes a small hop. Jump Height, Max Speed and Coyote Time are fields of Motion 2D.

The scene names a script by module, package and name. Here the module is game; in your project it is the module in project.aego.

3. A camera that follows

Select the camera and add Virtual Camera 2D and Camera Follow 2D. Drag the player from the Hierarchy onto the Target field of Camera Follow 2D. The camera now keeps the player in view and catches up smoothly; Deadzone and Damping set how loosely. See Cameras for zones, shake and blends.

4. A pit and a checkpoint

Split the floor in two with a gap between them. Under the gap, add a node Pit with a Kill Zone 2D whose Area covers the bottom of the level. Give the player Health and Respawn. Add a node Checkpoint past the pit with a Checkpoint 2D.

Falling into the pit kills the player. After Respawn's Delay it comes back at the last checkpoint it touched, or where it started, with full health.

5. Coins

A coin is a node with a Pickup 2D. It is collected when a node with Motion 2D enters its Area, and then removed. To count coins, give the player a counter:

package game

import (
	"aego/input"
	"aego/log"
)

script Player {
	var coins int

	func Update(dt float) {
		m := node.Get<Motion2D>()
		m.Move(input.Axis("Move"))
		if input.Pressed("Jump") {
			m.Jump()
		}
		if input.Released("Jump") {
			m.JumpReleased()
		}
	}

	func AddCoin() {
		coins++
		log.Info("coins: " + string(coins))
	}
}

and give each coin a script that tells the player:

package game

script Coin {
	func Ready() {
		node.Get<Pickup2D>().Collected.Connect(OnCollected)
	}

	func OnCollected(by NodeRef) {
		p := by.GetScript<Player>()
		if p != nil {
			p.AddCoin()
		}
	}
}

Make one coin, then turn it into a prefab (Create Prefab in the Hierarchy menu) and place copies of it; a change to the prefab reaches every copy. See Prefabs.

6. An enemy

An enemy walks back and forth and hurts the player on contact. Give it the same body as the player, a Motion 2D and a Hitbox 2D with Team 1, so it never hurts other enemies on team 1. Give the player a Hurtbox 2D: the hitbox damages the Health of the node whose hurtbox it touches. The player then blinks for Invuln Time and cannot be hit again until it ends.

The enemy turns around at walls:

package game

script Patrol {
	@export
	var Speed float = 0.5

	var dir float = 1

	func Ready() {
		node.Get<Motion2D>().HitWall.Connect(OnWall)
	}

	func OnWall(normal Vec2) {
		if normal.X != 0 {
			dir = normal.X
		}
	}

	func FixedUpdate(dt float) {
		node.Get<Motion2D>().Move(dir * Speed)
	}
}

HitWall gives the wall's normal, which points away from the wall, so its X is the new direction.

7. Play it anywhere

File → Export… builds the game for Linux, Windows, macOS or the web, as in Your first project. On a phone, the web build shows a touch gamepad: the stick moves and A jumps, because the actions are also bound to a gamepad.

Where to go next