I want to prevent the character from moving during the attack

Hello, my goal is to stay still until the swing animation stops and then move after the swing animation ends. But now, when you press the direction input key during an attack, it moves in a swing state.

extends CharacterBody2D

const SPEED : float = 30.0 
const RUN : float = 100.0 

@onready var animation_tree : AnimationTree = $AnimationTree

var direction : Vector2 = Vector2.ZERO 

func  _ready():
	animation_tree.active = true 
	
func _process(_delta):
	update_animation_parameters()

func  _physics_process(_delta):
	direction = Input.get_vector("left","right","up","down").normalized()
	
	if direction:
		if Input.is_action_pressed("Alt"):
			velocity = direction * SPEED
		else: 
			velocity = direction * RUN
	else:
		velocity = Vector2.ZERO
	
	move_and_slide()

func update_animation_parameters(): 
	
	if(velocity == Vector2.ZERO): 
		animation_tree["parameters/conditions/idle"] = true
		animation_tree["parameters/conditions/move"] = false
	else:
		animation_tree["parameters/conditions/idle"] = false 
		animation_tree["parameters/conditions/move"] = true
		if Input.is_action_pressed("Alt"): 
			animation_tree["parameters/move/conditions/is_moving"] = true
			animation_tree["parameters/move/conditions/is_run"] = false
		else: 
			animation_tree["parameters/move/conditions/is_run"] = true 
			animation_tree["parameters/move/conditions/is_moving"] = false

	if(Input.is_action_just_pressed("F")):
		animation_tree["parameters/conditions/swing"] = true
	else:
		animation_tree["parameters/conditions/swing"] = false


	if(direction != Vector2.ZERO):
		animation_tree["parameters/idle/blend_position"] = direction
		animation_tree["parameters/move/walk/blend_position"] = direction
		animation_tree["parameters/move/run/blend_position"] = direction
		animation_tree["parameters/swing/swing_01/blend_position"] = direction

make a new variable called can_walk. and using the is_playing() function on the swing animation and set the variable to true or false. then just use:

if direction and can_walk:
#code

maybe i’m wrong but i think this will work.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.