blob: cdd55d123a7943c370513be4340e54fdd641346c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
extends CharacterBody2D
@export var speed: float
@export var jump_strength: float
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
$AnimatedSprite2D.flip_h = (velocity.x < 0)
if not is_on_floor():
$AnimatedSprite2D.set_animation("jump")
elif abs(velocity.x) > 0.0:
$AnimatedSprite2D.set_animation("walk")
else:
$AnimatedSprite2D.set_animation("idle")
func _physics_process(delta: float):
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = -jump_strength
var direction = Input.get_axis("move_left", "move_right")
velocity.x = direction * speed
# Move the character
move_and_slide()
|