Menu
CoddyTech

Held: Zurück zum Start

Teil des Abschnitts Eingabe und Bewegung der Godot-Journey von Coddy. Lektion 27 von 28.

Wenn der Held in die Grube fällt, sollte er zum Start zurückkehren.

Speichere den Start in einer Variable in _ready(), die ausgeführt wird, bevor sich der Held bewegt:

var start_position = Vector2()

func _ready():
	start_position = position

Prüfe nach move_and_slide(), ob der Held unter den 360 Pixel hohen Bildschirm gefallen ist. Setze ihn zurück und setze seine Geschwindigkeit zurück, damit er nicht noch fallend ankommt:

if position.y > 400:
	position = start_position
	velocity = Vector2(0, 0)
challenge icon

Aufgabe

Mittel

Speichere die Startposition in _ready(). Wenn der Held unter y = 400 fällt, setze ihn dorthin zurück und setze seine Geschwindigkeit auf Vector2(0, 0).

Probier es selbst

extends CharacterBody2D

var speed = 220
var acceleration = 1200
var friction = 2400
var gravity = 1000
var jump_velocity = -500

func _physics_process(delta):
	velocity.y += gravity * delta
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity
	var direction = Input.get_axis("move_left", "move_right")
	if direction == 0:
		velocity.x = move_toward(velocity.x, 0, friction * delta)
	else:
		velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
	if direction > 0:
		$Look.scale.x = 1
	if direction < 0:
		$Look.scale.x = -1
	move_and_slide()
quiz iconTeste dich selbst

Diese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.

Alle Lektionen in Eingabe und Bewegung

Übe selbstständig: Godot-Playground