Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- const SPEED = 300.0
- const JUMP_VELOCITY = -400.0
- @onready var animation_player = $AnimationPlayer
- @onready var sprite = $Sprite2D
- var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
- func _physics_process(delta):
- if not is_on_floor():
- velocity.y += gravity * delta
- if Input.is_action_just_pressed("jump") and is_on_floor():
- velocity.y = JUMP_VELOCITY
- var direction = Input.get_axis("left", "right")
- if direction:
- velocity.x = direction * SPEED
- else:
- velocity.x = move_toward(velocity.x, 0, SPEED)
- if direction != 0:
- switch_direction(direction)
- move_and_slide()
- update_animations(direction)
- func update_animations(direction):
- if is_on_floor() and velocity.x == 0 :
- animation_player.play('Idle')
- else:
- animation_player.play('run')
- if velocity.y < 0:
- animation_player.play('jump')
- if velocity.y > 0:
- animation_player.play('fall')
- func switch_direction(direction):
- sprite.flip_h = (direction == -1)
- sprite.position.x = direction * 6
- func _process(delta):
- if Input.is_action_just_pressed('Attack'):
- animation_player.play('swing_nomove')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement