Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: A Top-Down Shooter

Build a top-down shooter in Godot 4: 8-direction movement, mouse aiming, bullet pooling, fire rate, enemies chasing with steering, health, waves and a score HUD, with Python simulations of the maths.

In this project you build a top-down arena shooter: the player moves with WASD, aims with the mouse, and shoots waves of enemies that chase them. It trains vector maths, scene composition, signals and the optimization habits from optimizing scenes. It builds directly on player movement and collision layers.

Godot cannot run on this site, so scripts and scene trees are read-only. The Python blocks are stand-ins that simulate the maths and rules (aiming, fire rate, waves), not the engine.

Scene layout

flowchart TD
    A["Arena (Node2D)"] --> P["Player (CharacterBody2D)"]
    P --> G["Muzzle (Marker2D)"]
    A --> B["Bullets (Node2D container)"]
    A --> E["Enemies (Node2D container)"]
    A --> S["WaveSpawner (Node + Timer)"]
    A --> H["HUD (CanvasLayer)"]

Containers for bullets and enemies keep the scene tree tidy and make "clear everything" trivial.

Physics layers: 1 world, 2 player, 3 enemy, 4 player_bullet. Bullets have layer 4 and mask 3 (enemies) plus 1 (walls). Enemies have layer 3 and mask 2 and 1. The player mask is 1 and 3.

Movement and aiming

Top-down movement has no gravity: set motion_mode = MOTION_MODE_FLOATING on the CharacterBody2D. Normalize the input so diagonals are not faster. Aim by rotating toward the mouse with look_at().

GDSCRIPT
# player.gd
extends CharacterBody2D

const SPEED := 260.0
const FIRE_INTERVAL := 0.15
@export var bullet_scene: PackedScene
var cooldown := 0.0

func _physics_process(delta: float) -> void:
	var dir := Input.get_vector("move_left", "move_right", "move_up", "move_down")
	velocity = dir * SPEED
	move_and_slide()
	look_at(get_global_mouse_position())

	cooldown -= delta
	if Input.is_action_pressed("shoot") and cooldown <= 0.0:
		cooldown = FIRE_INTERVAL
		shoot()

func shoot() -> void:
	var bullet := bullet_scene.instantiate()
	bullet.global_position = $Muzzle.global_position
	bullet.direction = Vector2.RIGHT.rotated(rotation)
	get_tree().current_scene.get_node("Bullets").add_child(bullet)

look_at() rotates the node so its local +X axis (right) faces the target, which is why the sprite should be drawn facing right and why the bullet direction is Vector2.RIGHT.rotated(rotation).

Python Stand-in (Python, not Godot): angle to the mouse and the bullet direction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Remember: in 2D, y grows downward, so a mouse below the player gives a positive angle.

Bullets

GDSCRIPT
# bullet.gd  (Area2D)
extends Area2D

const SPEED := 700.0
var direction := Vector2.RIGHT

func _ready() -> void:
	body_entered.connect(_on_body_entered)
	await get_tree().create_timer(1.5).timeout    # lifetime
	queue_free()

func _physics_process(delta: float) -> void:
	position += direction * SPEED * delta

func _on_body_entered(body: Node2D) -> void:
	if body.has_method("take_damage"):
		body.take_damage(1)
	queue_free()

At 700 pixels per second and 60 physics steps, a bullet moves about 11.7 pixels per step. A fast bullet could jump over a thin enemy between two steps (tunnelling). Either make bullets longer than their step, use a RayCast2D along the step, or increase the enemy's collision size. Use pooling (see the optimization lesson) if you fire many bullets.

Python Stand-in (Python, not Godot): fire rate limiter and tunnelling check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Enemies that chase

A simple chaser moves toward the player each frame. To avoid enemies stacking on one spot, add a little separation from neighbours.

GDSCRIPT
# enemy.gd
extends CharacterBody2D

signal died(points: int)
const SPEED := 90.0
var health := 3
var target: Node2D

func _ready() -> void:
	motion_mode = MOTION_MODE_FLOATING
	add_to_group("enemy")

func _physics_process(_delta: float) -> void:
	if not is_instance_valid(target):
		return
	var to_target := (target.global_position - global_position).normalized()
	velocity = to_target * SPEED
	move_and_slide()
	for i in get_slide_collision_count():
		var other := get_slide_collision(i).get_collider()
		if other == target and other.has_method("take_damage"):
			other.take_damage(1)

func take_damage(amount: int) -> void:
	health -= amount
	if health <= 0:
		died.emit(100)
		queue_free()

For maze-like levels use NavigationAgent2D with a NavigationRegion2D, which gives path-following around walls (the arena here is open, so straight-line chasing works).

Python Stand-in (Python, not Godot): chase with separation, then count the gap to the player

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Waves and difficulty

A WaveSpawner uses a Timer and a wave counter. Spawn enemies at random points on a ring outside the screen, increase the count and speed each wave, and give a short breather between waves.

GDSCRIPT
extends Node

@export var enemy_scene: PackedScene
var wave := 0
var alive := 0

func start_wave() -> void:
	wave += 1
	var count := 4 + wave * 2
	for i in count:
		var enemy := enemy_scene.instantiate()
		var angle := randf() * TAU
		enemy.global_position = Vector2(640, 360) + Vector2.RIGHT.rotated(angle) * 700.0
		enemy.target = get_tree().get_first_node_in_group("player")
		enemy.died.connect(_on_enemy_died)
		get_tree().current_scene.get_node("Enemies").add_child(enemy)
		alive += 1
		await get_tree().create_timer(0.3).timeout

func _on_enemy_died(points: int) -> void:
	alive -= 1
	GameState.add_score(points)
	if alive == 0:
		await get_tree().create_timer(2.0).timeout
		start_wave()
Python Stand-in (Python, not Godot): wave sizes and spawn positions on a ring

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

HUD, game over and polish

The HUD shows health, score and wave. Listen to signals from the player and GameState. On death, pause with get_tree().paused = true, show a "Game Over" panel with a Retry button (its process_mode set to Always so it still works while paused) and reload_current_scene(). Polish: muzzle flash, hit sparks, enemy knockback, screen shake, sound on each shot with pitch variation, a high score saved to disk (save and load).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which motion mode suits a top-down CharacterBody2D without gravity?

    MOTION_MODE_FLOATING has no floor concept, so movement is the same in all directions.
  2. 2. Why normalize the input vector (or use Input.get_vector)?

    Pressing two keys gives a vector of length about 1.41 unless it is normalized or limited.
  3. 3. What is bullet tunnelling?

    Per step movement bigger than the target means the overlap is never detected.
  4. 4. Why use containers (Bullets, Enemies) in the Arena scene?

    Grouping spawned objects under a node keeps the tree readable and lets you free them all at once.

Summary

  • Floating motion mode and Input.get_vector() give top-down movement; look_at() aims at the mouse.
  • Fire rate is a cooldown timer; fast bullets need tunnelling protection.
  • Chasers move toward the player with a little separation; use navigation for mazes.
  • A wave spawner raises counts and speeds; signals connect enemies to the score and HUD.

Next: Project: A Save and Load System