• 2D
  • GDScript needed for movement of sprite while undergoing animation.

Hello, I have resumed my work on my game. But since I'm new to GDScript, I still have a lot to learn. Now, I want to move my character while it undergoes animation. I have used the Animated Sprite node to create a running animation. But the script I have used is for controlling the animation, so when I leave the button needed to animate the character, the animation stops (not completed). This is the script, taken from the Godot Docs:

extends KinematicBody2D

onready var _animated_sprite = $AnimatedSprite

func _process(_delta):
    if Input.is_action_pressed("ui_right"):
        _animated_sprite.play("run")
    else:
        _animated_sprite.stop()

The animation is titled 'run'. As mentioned above, this one's used for controlling. I need a code for complete animation on one press (no holding). Additionally, I need code required to move the character while it is animated by X-axis distance. Since, I am not much experienced with GDScript, I really need help.

Thank you for reading.

The mentioned code is in Italic, just to show you. However, looks like the entire code is not in Italic. Sorry.

You'll need to move the kinematic body itself. For that you'd need to modify the position of the body itself.

But in addition the run animation should only be triggered with is_action_just_pressed() which returns true when the action is started.

use is_action_pressed() to update body.position (check docs for exact property)

so something like this if action just pressed: start run animation (animations can be made a loop, so you only have to start it) else action just released: stop animation (stops animation when button is released) if action is_pressed: position.x += move_increment (only updates when button is down)

I would recommend exploring the many tutorials available on godot basics. GDQuest is a good youtube channel for it. Also the kids can code site/channel. Despite its name, it offers solid clear introduction and tutorials for godot.

@BlueLightningWizard said: The mentioned code is in Italic, just to show you. However, looks like the entire code is not in Italic. Sorry.

You might want to look through the second post here to have a better idea of how you can format your posts. ;)

10 days later