Hi,
I am attempting to write a movement system in 2D for an isometric game using animation_tree.
The problem I am encountering is the player can move during the attack animation, whereas I would like the player to stay still.
I know I need to await (yield) until the animation is finished, however, if I use await $AnimationPlayer.animation_finished
it yields forever.
What should I be using instead?
extends CharacterBody2D
@onready var animation_tree = get_node("AnimationTree")
@onready var animation_mode = animation_tree.get("parameters/playback")
var max_speed = 400
var speed = 0
var acceleration = 1200
var move_direction = Vector2(0,0)
var moving = false
var attacking = false
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func _physics_process(delta):
MovementLoop(delta)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _unhandled_input(event):
if event.is_action_pressed('CLICK'):
moving = false
attacking = true
print(position.direction_to(get_global_mouse_position()))
animation_tree.set('parameters/Melee/blend_position', position.direction_to(get_global_mouse_position()).normalized())
animation_tree.set('parameters/Idle/blend_position', position.direction_to(get_global_mouse_position()).normalized())
Attack()
func MovementLoop(delta):
if attacking == true:
return
move_direction.x = int(Input.is_action_pressed("WALK_RIGHT")) - int(Input.is_action_pressed("WALK_LEFT"))
move_direction.y = (int(Input.is_action_pressed("WALK_DOWN")) - int(Input.is_action_pressed("WALK_UP"))) / float(2)
if move_direction == Vector2(0,0):
animation_mode.travel("Idle")
speed = 0
else:
speed += acceleration * delta
if speed > max_speed:
speed = max_speed
var movement = move_direction.normalized() * speed
velocity = movement
animation_tree.set('parameters/Walk/blend_position', movement.normalized())
animation_tree.set('parameters/Idle/blend_position', movement.normalized())
animation_mode.travel("Walk")
move_and_slide()
func Attack():
animation_mode.travel("Melee")
#await here
attacking = false