Fire the Engine
Part of the Projects section of Coddy's Godot journey. Lesson 11 of 33.
Up fires the engine and pushes the ship the way its nose points. Vector2.UP is (0, -1). rotated(rotation) turns that arrow as far as the ship has turned:
if Input.is_action_pressed("move_up"):
velocity += Vector2.UP.rotated(rotation) * THRUST * delta
position += velocity * deltaThe engine changes the velocity, not the position. When you let go of Up, the ship keeps drifting. To stop, turn around and thrust the other way.
The ship has a hidden Flame child. Show it while the engine fires: $Flame.visible = Input.is_action_pressed("move_up").
Challenge
Medium- Add
const THRUST = 300. - While Up is held, add
Vector2.UP.rotated(rotation) * THRUST * deltato the ship'svelocity. - Move the ship by
velocity * deltaevery frame, held or not. - Show
$Flameonly while Up is held.
Try it yourself
extends CharacterBody2D
const TURN_SPEED = 4.0
func _physics_process(delta):
rotation += Input.get_axis("move_left", "move_right") * TURN_SPEED * delta
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
2Project: Asteroid Shooter
Turn the ShipFire the EnginePractice on your own: Godot playground