+2 votes

I need to change an animation's length through code. I also want the keys to be scalable. I haven't found any methods for changing an animation's length. Can I do that?

in Engine by (399 points)

3 Answers

+2 votes
Best answer
$AnimationPlayer.get_animation('YourAnimation').length = new_length
by (4,077 points)
selected by

Hahah I tried get_current_animation().get_length() but it didn't worked. However, get_animation("").length worked. Thank you

try $AnimationPlayer.current_animation_length

+1 vote

Length of your animation should be set within animation node, not the player. Try something like this.

var length = 3.0

var    your_animation = Animation.new()
your_animation.add_track(0)
animation_float.track_set_path(0, "Sprite:position")
your_animation.track_insert_key ( 0, 0, Vector2(0, 0))
your_animation.track_insert_key ( 0, length, Vector2(128, 0))

Basicly you create custom length variable and assign it to time argument when inserting new key to track. With continous updateMode it should work with any amout of frames, if Im not mistaken.

After animation is set, you add it to your player by add_animation() function.

Have a research here: https://docs.godotengine.org/en/3.0/classes/class_animation.html?highlight=add_track

by (102 points)
+3 votes

You can change the speed of animation:

var speed = 0.5
$AnimationNode.playback_speed = speed

If the original Animation length is 5 seconds and you change speed to 0.5, the animation will play 10 seconds (half speed). If you change speed to 2, the animation will play at double speed and the lenght will be 2.5 seconds. 1 is for normal speed.

by (20 points)
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.