Skip to content

Instantly share code, notes, and snippets.

@WolfgangSenff
Created February 20, 2024 16:03
Show Gist options
  • Save WolfgangSenff/bc429effac2f97f1c3cfa5ff7684b06c to your computer and use it in GitHub Desktop.
Save WolfgangSenff/bc429effac2f97f1c3cfa5ff7684b06c to your computer and use it in GitHub Desktop.
For Bubs
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