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 :

Answers

  • RevampedSpiderRevampedSpider Posts: 6Member

    Bump.

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    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():
        play(idle_anim_name);
    func _process():
        if (Input.is_action_pressed("ui_right")):
            velocity.x = move_speed;
        elif (Input.is_action_pressed("ui_left")):
            velocity.x = -move_speed;
        else:
            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;
        else:
            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):
                play(moving_anim_name);
        else:
            if (animation != idle_anim_name):
                play(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: 1,049Admin

    @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: 1,049Admin

    @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:

Sign In or Register to comment.