Problems with enemies

Godot Version

4.4.1

Hello, I encountered a weird problem when trying to target enemies.
Player’s direction starts glitching when targeting an enemy

Here is the code I use on the Player code:

func _physics_process(delta):
	super._physics_process(delta)
	move_speed = 0.25
	
	if moving == false:
		animation_tree["parameters/conditions/idle"] = true
		animation_tree["parameters/conditions/is_moving"] = false
		if attacking == true:
			animation_tree["parameters/conditions/attacking"] = true
			animation_tree["parameters/attack/blend_position"] = direction
		else:
			animation_tree["parameters/conditions/attacking"] = false
	else:
		animation_tree["parameters/conditions/idle"] = false
		animation_tree["parameters/conditions/is_moving"] = true
		animation_tree["parameters/idle/blend_position"] = direction
		animation_tree["parameters/walk/blend_position"] = direction
		if attacking == true:
			animation_tree["parameters/conditions/attacking"] = true
			animation_tree["parameters/attack/blend_position"] = direction
		else:
			animation_tree["parameters/conditions/attacking"] = false

Another minor problem would be that when I tap on another enemy the targeting enemy sprite doesn’t get removed from the previous enemy and I don’t really know how to do that.

func _on_enemy_body_input_event(_viewport: Viewport, event: InputEvent, _shape_idx: int) -> void:
	if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
		player.attacking = !player.attacking
		if player.attacking == true:
			player.id_path = map.find_path(player.global_position, enemy_location) #This is how I set path to the enemy
			target_enemy = load("res://Target_enemy.tscn").instantiate()
			add_child(target_enemy)
		else:
			remove_child(target_enemy)

I think that the reason the animation is bugged is due to the fact that there are two conditions that return true which makes the animation tree rapidly switch between the two animations. I would check for this by adding this to the process function:

print("Idle" : , animation_tree["parameters/conditions/idle"])
print("Is moving" : , animation_tree["parameters/conditions/is_moving"])
print("Attacking" : , animation_tree["parameters/conditions/attacking"])

I would guess that idle and attacking are returning true at the same time (It’s hard to pinpoint if the issue is with the direction when you don’t show how direction is being defined).

Also to remove a child you can use the queue_free() method.
This would look like so target_enemy.queue_free()
I think atleast. I’m not very good with untested code

No need to print since it’s possible to see conditions in the remote. When attacking 2 conditions are true. Attacking + Idle or Attacking + Walking

Yeah, i think you just need to add animation_tree["parameters/conditions/idle"] = false and animation_tree["parameters/conditions/is_moving"] = false to the if is_attacking:.