0 votes

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


Godot version 3.3.3.stable
in Engine by (16 points)
retagged by

1 Answer

0 votes

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.

by (7,190 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.