Play correct animation and direction depending on player speed and mouse position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GrumpyCJ

hi. after getting hundreds of flip or rotate results (which is not what i want) i have decided to ask for help here. I want my player’s animations to play based on keyboard input and mouse, like the following:

if WASD not pressed = idle animation
else WASD pressed = toggle walk animation
…if SHIFT pressed = toggle run animation
(walk/run enabled with keyboard input, idle if no input/speed)

then, depending on the player’s speed/input and where the mouse is located, the sprite would play the correct animation to the corresponding direction

the project is an isometric topdown shooter 2D
all animations are in AnimationTree BlendSpace2D as:
Idle/Walk/Run + “_” + E, N, NE, NW, S, SE, SW, W


:bust_in_silhouette: Reply From: exuin

For handling the AnimationTree stuff, check out HeartBeast’s RPG tutorial if you haven’t already. I’m not sure which video exactly does the state machine stuff, but it’s basically just using if statements to control what animation is played.

As for the direction the player is facing, call Vector2.ZERO.direction_to(get_local_mouse_position()) to get the Vector2 representing the direction to the mouse cursor. Have the state machine travel to that position to get the correct animation.

Hi. After giving it a try and doing some more research this is the result after different attemps.
I leave the code I am using for my initial question. I might as well say that I am using an AnimationNodeBlendTree

func _process(delta): # PROCESS ////////////////////////
func _animLoop(delta):
if moveDir != Vector2():
	if Input.is_action_pressed("SPRINT"): # If baseSpeed = walkSpeed * runSpeed
		$animTree.set("parameters/animState/current", 2) # RUN
	else: # If baseSpeed = walkSpeed
		$animTree.set("parameters/animState/current", 3) # WALK
else: # If moveDir = Vector2()
	$animTree.set("parameters/animState/current", 1) # IDLE

func _mouseLoop(delta):
var mousePos = Vector2().direction_to(get_global_mouse_position().normalized())

if moveDir != Vector2():
	if Input.is_action_pressed("SPRINT"): # If baseSpeed = walkSpeed * runSpeed
		$animTree.set("parameters/runState/blend_position", mousePos) # RUN
	else: # If baseSpeed = walkSpeed
		$animTree.set("parameters/walkState/blend_position", mousePos) # WALK
else: # If moveDir = Vector2()
	$animTree.set("parameters/idleState/blend_position", mousePos) # IDLE

The animations are called properly for input (movement) Vector2(), or so i believe… but as for the mouse, well… it’s not working properly. The Idle animation seems to play flawless except the fact that somehow, it’s playing inverted at N, NE, NW & S, SE, SW, that’s it if the player is where it “spawned” in the debugger… now if i move around that changes as if the “center” is left behind.
Run and Walk animations don’t update properly, i need to tap the movement for it to change (update) directions.
I have been trying to fix things around google searches and youtube videos but i’m clearly missing something in the middle and i can’t see what.

I leave a video showing the issue.

GrumpyCJ | 2021-09-17 22:48