0 votes

This question is based off of the animationplayer node for 3D.

Is there a way to make particular key frames in an animation based off a script?

For example, I want an animation, that effects the nodes translations, to end where it started before it ever ran through the series of animations I have it run.

Essentially, I'd like a key frame that was a type of transform.origin key. This way I can send the node to animate in response to what is happening, but then at the end of the last animation that plays, it can return back to where it was smoothly.

in Engine by (293 points)

1 Answer

+1 vote
Best answer

You can use $AnimationPlayer.get_animation() to get the Animation then call track_set_key_value() on that, plus other functions to get the track or the last_key:

var animation_name = $AnimationPlayer.current_animation # or some other name
var animation = $AnimationPlayer.get_animation(animation_name)

var track_name = 'Spatial:translation' # replace with your track name
var track = animation.find_track(track_name) # or an integer
var last_key = animation.track_get_key_count(track) - 1

animation.track_set_key_value(track, last_key, original_position)
by (4,005 points)
selected by

This is perfect, thank you.

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.