How do I play an AnimatedSprite3D when I am moving?

RevampedSpiderRevampedSpider Posts: 6Member

I'm making a game in a style similar to Paper Mario. I set up and player's movement and sprites but I need to play the walk animation when he moves. My variable is "is_moving".

How do I do this?

Tags :


  • RevampedSpiderRevampedSpider Posts: 6Member


  • TwistedTwiglegTwistedTwigleg Posts: 3,557Admin

    In theory, you just need to do something like what I wrote below. Keep in mind, I have not tested it so I have no idea if it works or not.

    extends AnimatedSprite3D
    var moving_anim_name = "moving";
    var idle_anim_name = "idle";
    var velocity = Vector3(0,0,0);
    var move_speed = 20;
    func _ready():
    func _process():
        if (Input.is_action_pressed("ui_right")):
            velocity.x = move_speed;
        elif (Input.is_action_pressed("ui_left")):
            velocity.x = -move_speed;
            velocity.x = 0;
        if (Input.is_action_pressed("ui_up")):
            velocity.z = move_speed;
        elif (Input.is_action_pressed("ui_down")):
            velocity.z = -move_speed;
            velocity.z = 0;
        global_translate(velocity * delta)
        var h_vel = velocity;
        h_vel.y = 0;
        if (h_vel.length() > 0):
            if (animation != moving_anim_name):
            if (animation != idle_anim_name):

    The general idea is pretty simple though. You just need to track whether the player is moving, and then if the player is moving, change animations. I used velocity to track movement, but you could use a different method if you'd rather.

    You could instead replace if (h_vel.length() > 0): with if (is_moving == true): for example. So long as is_moving is true when the player is moving, and false when they are not, then it should work just fine.

    Hopefully this helps! :smile:
    (Also, please try to avoid posting just to bump a topic if possible)

  • RevampedSpiderRevampedSpider Posts: 6Member

    I just started trying this after making small projects and the only problem is "Identifier: Delta not found".

  • TwistedTwiglegTwistedTwigleg Posts: 3,557Admin

    @RevampedSpider said:
    I just started trying this after making small projects and the only problem is "Identifier: Delta not found".

    Looking at the code, I simply forgot to add delta as a argument in the _process function. Change func _process(): to func _process(delta): and then, hopefully, it will work :smile:

  • RevampedSpiderRevampedSpider Posts: 6Member

    Thanks! It works. Is there any way I can add collision and gravity though?

  • TwistedTwiglegTwistedTwigleg Posts: 3,557Admin

    @RevampedSpider said:
    Thanks! It works. Is there any way I can add collision and gravity though?

    Great, I’m glad it is working!

    As for adding collision and gravity, I would suggest either using a RigidBody2D node, or a KinetmaticBody2D node. Personally of the two I would use the KinematicBody2D node as it is a little easier to control.

    There is a page in the documentation on how to setup a simple character using a KinematicBody2D node that may be of some help. You can also find a 2D platformer demos on the Godot demo repository that show how to make a simple platformer character using either the KinematicBody2D node or the RigidBody2D node.

    Hopefully this helps :smile:

  • KindosaurGamingKindosaurGaming Posts: 65Member
    edited October 2019

    Hey um,
    I am not sure if you will still react to this, but I still want to take a shot at this.

    Did you eventually manage to make your Sprite Move in 8 directions or only in 4 (the normal UP, DOWN, LEFT, RIGHT)? If you got the 8 directions, can you maybe explain or show how that is done cuz for the life of me can't get through this and I really want to make progress on my game.

    I have made a couple of posts over the internet about this, with no succes so I thought "maybe the source of where my question got partially answered knows more."


  • TwistedTwiglegTwistedTwigleg Posts: 3,557Admin
    edited October 2019

    If you do not mind me asking, what type of movement are you looking for? Do you mean eight directions like the normal (up/down/left/right) and the four diagonals (up-left/up-right/down-left/down-right)?

    I tested the code I posted above in a sample project and changed the movement speed so I could see what was going on. Pressing multiple directions seems to work, as the character moves diagonally. The movement is not relative to the Camera though, but it does seem to move diagonally when left/right and up/down are pressed together.

    I'm not sure if that is what you are looking for though.

    If you mean eight directions like adding gravity/collision, then you'll need to use 3D collision nodes. I would suggest using a KinematicBody node with a small box collision shape, and movement code similar to the KinematicCharacter demo on the Godot demo repository.

    (Unless your game is 2D, in which case what I wrote above about using an RigidBody2D/KinematicBody2D node would work. If the game is 3D though, which I assume it is since you are using an AnimatedSprite3D node, you will need to use a 3D physics node. I actually replied with the wrong information in my earlier reply... Whoops :sweat_smile:).

  • KindosaurGamingKindosaurGaming Posts: 65Member
    edited October 2019

    Hi TwistedTwigleg,
    About the 8 directions: I mean Up,Down,Left,Right,UpLeft,UpRight,DownLeft,DownRight. I really want to explain it here but I already have a whole post for this question and with a lot of detail in it about what I see when using the code and also what kind of game I am trying to make :)

    So in short what I wrote was:

    My game is a 2.5D (as in a 2D sprite in a 3D world) kinda like a Paper Mario*Banjo Kazooie mix

    I want to use 8 directions (maybe 16 in the future)

    When I used the code I managed to get some movement with the button presses needed, so my character stood still (as I like to see cuz it was his idle animation :) ) and when pressing Up it started his MovingForward animation (that is what I wanted as well :) ). But when pressing Left, Right or Down the animation of these movements(animations) didn't work.

    Here is the link for the full post of mine with also the things I changed to the code you gave. I have been changing it a bit after that post. But I am still stuck :'(

    here is the link of my post:

    Cuz I was also wondering
    if I say the following:
    if (Input.is_action_pressed("Walk_Forward") and (Input.is_action_pressed("Walk_Right"))):
    does this work as a indicator to use the animation ForwardRight (in my case) or is this just not working


  • TwistedTwiglegTwistedTwigleg Posts: 3,557Admin

    Okay, thanks for clarifying!
    I'll take a look at the code on your post and see if I can help figure out what is going on! :smile:

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file