Fire the Engine
Part of the Projects section of Coddy's Godot journey. Lesson 12 of 36.
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
MediumMake the engine push the ship.
- Add
const THRUST = 300. - While Up is held, add
Vector2.UP.rotated(rotation) * THRUST * deltatovelocity. - Move the ship by
velocity * deltaevery frame, held or not. - Show
$Flameonly while Up is held.
Press Play and hold Up: the flame burns and the ship drifts on after you let go. Then Check.
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
All lessons in Projects
2Project: Asteroid Shooter
Turn the ShipFire the EnginePractice on your own: Godot playground