Created
February 20, 2024 16:03
-
-
Save WolfgangSenff/bc429effac2f97f1c3cfa5ff7684b06c to your computer and use it in GitHub Desktop.
For Bubs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends CharacterBody2D | |
# ... variables | |
@onready var anim = $AnimationPlayer | |
@onready var sprite = $Sprite2D # Both assume you have an AnimationPlayer node as a child, and Sprite2D | |
var current_animation = "idle" | |
func _physics_process(delta: float) -> void: | |
current_animation = "idle" | |
var input = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") | |
if input.x > 0 and sprite.scale.x != 1: | |
sprite.scale.x = 1 | |
current_animation = "run" | |
elif input.x < 0 and sprite.scale.x != -1: | |
sprite.scale.x = -1 # Make sure that, in the editor, you break the "chain" next to the sprite's scale by clicking the link icon | |
current_animation = "run" | |
if input.y < 0: # check for jump | |
current_animation = "jump" | |
velocity.y = -JUMP_STRENGTH # Whatever strength you define this to be | |
if velocity.y > 0: | |
current_animation = "fall" | |
velocity.y += gravity * delta | |
velocity.x = input.x | |
if anim.current_animation != current_animation: | |
anim.play(current_animation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment