Hey everybody,
i want to make my 2D Character to walk Left and right (That works fine) he also needs to walk up and down but also facing left or right. Whenever he walks vertically he will always face to the left Ddirection, but i want him to face in the direction he looked before going vertically.

extends CharacterBody2D


const ACCELERATION = 1000
const MAX_SPEED = 150
const FRICTION = 1000
const ROLL_SPEED = 200

enum{
	MOVE,
	ROLL,
	ATTACK
}

var state = MOVE
var roll_vector = Vector2.DOWN


@onready var animationPlayer = $AnimationPlayer
@onready var animationTree = $AnimationTree
@onready var animationState = animationTree.get("parameters/playback")


func _ready():
	animationTree.active = true


func _physics_process(delta):
	match state:
		MOVE:
			move_state(delta)
		ROLL:
			roll_state()
		ATTACK:
			attack_state(delta)


func move_state(delta):
	
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	input_vector = input_vector.normalized()
	
	if input_vector != Vector2.ZERO:
		roll_vector = input_vector
		animationTree.set("parameters/Idle/blend_position", input_vector)
		animationTree.set("parameters/Run/blend_position", input_vector)
		animationTree.set("parameters/Attack/blend_position", input_vector)
		animationTree.set("parameters/Roll/blend_position", input_vector)
		animationState.travel("Run")
		velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
	else:
		animationState.travel("Idle")
		velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
	move()
	
	if Input.is_action_just_pressed("Roll"):
		state = ROLL
	
	if Input.is_action_just_pressed("attack"):
		state = ATTACK

func roll_state():
	velocity = roll_vector * ROLL_SPEED
	animationState.travel("Roll")
	move()

func attack_state(delta):
	animationState.travel("Attack")
	velocity = velocity.move_toward(Vector2.ZERO, FRICTION/2 * delta)
	move_and_slide()

func move():
	move_and_slide()

func roll_animation_finished():
	velocity = roll_vector * MAX_SPEED
	state = MOVE
func attack_animation_finished():
	state = MOVE

This is the Script i am using from another project. and in the Pictures you can see my animation tree setup.
I would be very thankfull if someone could help me. Thank you!!




8 days later

Take what I will say with a grain of salt as I am not really sure about it, but it might be worth something.
So when you are moving up, your input_vector is (0, -1), and then setting the blend_position to just that value. Inside the blend space, that point (0, -1), is as far from (-1, 0) with the left animation, as it is far from (1, 0) with the right animation. The same goes for (0, 1) for moving down, I believe.
So I think the engine defaulted to the (-1, 0) somehow, and you only see the left animation.
What I suggest doing, since you don't have up or down animations, is to only set the blend_position when input_vector.x != 0
So inside the move_state(), inside the if input_vector != Vector2.ZERO:

if input_vector.x != 0:
    animationTree.set("parameters/Idle/blend_position", input_vector)
    animationTree.set("parameters/Run/blend_position", input_vector)
    animationTree.set("parameters/Attack/blend_position", input_vector)
    animationTree.set("parameters/Roll/blend_position", input_vector)

I hope this helped