obsidian/MyObsidianVault/Godot_Training/2D/Tutorial Game 1 - to space and beyond/02. Player Input.md
2024-03-07 17:47:56 +01:00

557 B

https://school.gdquest.com/courses/learn_2d_gamedev_godot_4/to_space_and_beyond/taking_control

extends Sprite2D
var direction := Vector2(0,0)
var max_speed := 600.0
var velocity = Vector2(0, 0)

func _process(delta: float) -> void:
	
	direction.x = Input.get_axis("move_left", "move_right")
	direction.y = Input.get_axis("move_up", "move_down")
	
	if direction.length() > 1.0:
		direction = direction.normalized()
	
	velocity = direction * max_speed
	position += velocity * delta
	if direction.length() > 0.0:
		rotation = velocity.angle()