extends CharacterBody2D @export var speed :float = 100.0 @export var jump_velocity = -200.0 @onready var aps = $AnimatedSprite2D # Get the gravity from the project settings to be synced with RigidBody nodes. var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") var animation_locked : bool = false var direction : Vector2 = Vector2.ZERO func _physics_process(delta): # Add the gravity. if not is_on_floor(): velocity.y += gravity * delta # Handle jump. if Input.is_action_just_pressed("ui_accept") 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. direction = Input.get_vector("left", "right","up", "down" ) if direction: velocity.x = direction.x * speed else: velocity.x = move_toward(velocity.x, 0, speed) move_and_slide() update_animation() update_facing_direction() func update_animation(): if not animation_locked: if direction.x != 0: aps.play("run") else: aps.play("idle") func update_facing_direction(): if direction.x > 0: aps.flip_h = false elif direction.x < 0: aps.flip_h = true func jump(): velocity.y = jump_velocity aps.play("jump") animation_locked = true
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter