-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.gd
More file actions
31 lines (25 loc) · 962 Bytes
/
player.gd
File metadata and controls
31 lines (25 loc) · 962 Bytes
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
extends CharacterBody2D
@onready var animated_sprite_2d = $AnimatedSprite2D
const SPEED = 200.0
const JUMP_VELOCITY = -400.0
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("left", "right")
if direction:
# If direction changes, flip sprite horizontally
animated_sprite_2d.flip_h = false if direction == 1 else true if direction == -1 else animated_sprite_2d.flip_h
velocity.x = direction * SPEED
animated_sprite_2d.play("walk")
else:
velocity.x = move_toward(velocity.x, 0, SPEED/20)
# Stop animation if no velocity
if velocity.x == 0:
animated_sprite_2d.stop()
move_and_slide()