How do I attach animations to movements (and non movements)

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

I’m new to Godot but I’ve worked with python before and I’m working on a project over my break. I have a sprite sheet and have tried sprite and animatedsprite to do this but I can’t seem to figure out how to get either to display 4 walking animations and 4 idle animations. I think what I need is some function to check if the player is moving, but I don’t know how to set that up (side note: what does it mean to put Vector2 in your script???)… Just in case I haven’t been clear, the outcome I want is for the player to have a movement animation corresponding to up down left right and when the player has finished moving the sprite will stay on the idle frame corresponding to the direction they were traveling last.

hi, Have you already checked the “Your First Game” tutorial on godot documentation? there it shows how to select animation based on movement.

what does it mean to put Vector2 in your script???

I don’t even know what the question means without a context. This is something someone told you to do? I think it could mean to define a variable of type Vector2 in your script, for using it for some purpose…

p7f | 2018-12-26 01:18

:bust_in_silhouette: Reply From: Shawk

I believe the way to do this is to just put the code to start the animation at the same place you tell the program to do something. For example, say you have:

if Input.is_action_pressed("move_left"):
    velocity.x = -1 * speed
elif Input.is_action_pressed("move_right"):
    velocity.x = 1 * speed
elif Input.is_action_pressed("move_up"):
    velocity.x = -1 * speed
elif Input.is_action_pressed("move_down"):
    velocity.x = 1 * speed

Just add the code to animate it with the code to move it in the first place. So in this case, it’d be…

if Input.is_action_pressed("move_left"):
    velocity.x = -1 * speed
    $AnimatedSprite.play("walk_left")
elif Input.is_action_pressed("move_right"):
    velocity.x = 1 * speed
    $AnimatedSprite.play("walk_right")
elif Input.is_action_pressed("move_up"):
    velocity.x = -1 * speed
    $AnimatedSprite.play("walk_up")
elif Input.is_action_pressed("move_down"):
    velocity.x = 1 * speed
    $AnimatedSprite.play("walk_down")
else:
    $AnimatedSprite.play("idle")

Obviously the code won’t be exactly the same as what I put here, and you’ll need to set some other stuff up (like the input map so the computer knows what "move_up", "move_left", etc. means, and the AnimatedSprite animations (which I’m assuming you’ve already done), or the velocity vector and use it for move_and_slide() or whatever you’re using to move) so if you need help with that just reply to this post or hit me up on Discord: Shawk#8857

Also, about the vectors, in Godot a vector is basically a variable but with two or three values (depending on if it’s 2D or 3D) that is usually used for physical positions. Again, this can be a bit confusing so reply to this post or send me a Discord message if you need help.

:bust_in_silhouette: Reply From: code

Actually you can use this method
U just have to create a seperate method and give it a name let say “animation_manager” which u will call in ur process or physics_process method then u have to create boolean variables for all action that the player takes such as running sliding jumping falling attacking etc for example

if Input.is_action_pressed("ui_left"):
      "Run to the left"
        Running = true

if Input.is_action_pressed("ui_right"):
      "Run to the right"
        Running = true

If Input.is_action_just_released("ui_left") or Input.is_action_just_released("ui_right"):
   running = false

Then you do same for jump and sliding i will recommend for u to have use of enum and array for easy data arrangement for your animation_manager method the code went like this

Func animation_manager():
If running and not ( jumping or sliding):
     "Play run animation"
If jumping:
    "Play jump animation"
// for idle u just have to check if all the states are false then it mean that the player is truly idle"

That the code am currently using am also new to game development as a whole and am still looking for the best way to manage my animation but this works perfectly for now

And sorry if my explanation is not so good am not really good at explaining
Thanks