Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- const SPEED = 120.0
- func _physics_process(delta):
- #input & animations handling
- var movement = Vector2()
- movement.x = Input.get_axis("left","right")
- movement.y = Input.get_axis("up","down")
- movement = movement.normalized()
- if movement != Vector2.ZERO:
- $RayCast2D.target_position = movement * 32
- velocity = movement * SPEED
- update_animation(movement)
- move_and_slide()
- func get_direction_as_string():
- #converting vector to string to simplify the update_animation() function
- var dir = $RayCast2D.target_position
- if dir.x > 0:
- return "right"
- elif dir.x < 0:
- return "left"
- elif dir.y > 0:
- return "up"
- elif dir.y < 0:
- return "down"
- func update_animation(movement):
- #switching UP/DOWN to play "run_right" animation
- var dir = get_direction_as_string()
- if dir == "up" or dir == "down":
- dir = "right"
- #update sprite animation based on movement direction
- if movement != Vector2.ZERO:
- $AnimatedSprite2D.play("run_"+dir)
- else:
- $AnimatedSprite2D.play("idle")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement