Newest Version
So I have a top down game. Im trying to add a roll mechanic where the player can press a button, it will then boost the player in the direction they are facing. The problem is I want to resume normal movement once the roll animation is finished, but it wont detect when its finsihed
CODE:
extends CharacterBody2D
const speed = 75
var accel = 5000
var friction = 900
var is_rolling = false
var roll_speed = 300
var roll_direction = Vector2.ZERO # Variable to store roll direction
var input = Vector2.ZERO
func _physics_process(delta):
if Input.is_action_just_pressed("roll") and not is_rolling:
roll_direction = get_roll_direction()
is_rolling = true
velocity = roll_speed * roll_direction # Set initial velocity
$AnimationPlayer.play("Roll")
elif is_rolling:
velocity = roll_speed * roll_direction # Update velocity each frame during roll animation
else:
if velocity.x != 0 or velocity.y != 0:
$AnimationPlayer.play("Walk")
else:
$AnimationPlayer.play("Idle")
if Input.is_action_pressed("left"):
$Sprite2D.flip_h = true
if Input.is_action_pressed("right"):
$Sprite2D.flip_h = false
input = get_input()
if input == Vector2.ZERO:
if velocity.length() > (friction * delta):
velocity -= velocity.normalized() * (friction * delta)
else:
velocity = Vector2.ZERO
else:
velocity += (input * accel * delta)
velocity = velocity.limit_length(speed)
move_and_slide()
func _on_animation_finished(anim_name: String, anim_finished: bool):
if anim_name == "Roll":
is_rolling = false
func get_input():
input.x = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"))
input.y = int(Input.is_action_pressed("down")) - int(Input.is_action_pressed("up"))
return input.normalized()
func get_roll_direction():
if Input.is_action_pressed("down"):
return Vector2.DOWN
if Input.is_action_pressed("up"):
return Vector2.UP
if Input.is_action_pressed("left"):
return Vector2.LEFT
if Input.is_action_pressed("right"):
return Vector2.RIGHT
else:
if $Sprite2D.flip_h == true:
return Vector2.LEFT
else:
return Vector2.RIGHT