Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | jjphung | |
Old Version | Published before Godot 3 was released. |
The animation only triggers when I let go of the button and not while it is moving. Can not find a function for is_action_hold
or something like that.
extends KinematicBody2D
const GRAVITY = 200
const WALK_SPEED = 250
var velocity = Vector2()
var sprite
var animPlayer
func _ready():
sprite = get_node("Sprite")
animPlayer = get_node("AnimationPlayer")
set_fixed_process(true)
func _fixed_process(delta):
velocity.y += delta * GRAVITY
if (Input.is_action_pressed("ui_left")):
velocity.x = -WALK_SPEED
sprite.set_frame(0)
animPlayer.play("walk_left")
elif (Input.is_action_pressed("ui_right")):
velocity.x = WALK_SPEED
sprite.set_frame(4)
animPlayer.play("walk_right")
else:
velocity.x = 0
var motion = velocity * delta
motion = move(motion)
if (is_colliding()):
var n = get_collision_normal()
motion = n.slide(motion)
velocity = n.slide(velocity)
move(motion)