How do I play only a segment of AnimationPlayer?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CakeLover

The title says it all

I have an AnimationPlayer node with 8 frames and I only want to play from 0 to 0.4
(4 frames)


I was hoping there might be a solution like $"Run",start=0,end=0.4)

is there any way to achieve the desired result?

start :
$“strart:float”, true)

look AnimationPlayer “Doc” = document

but I don’t know the ending

ramazan | 2022-01-18 11:14

:bust_in_silhouette: Reply From: DaddyMonster

Not certain that this is the optimal method but it could certainly be done this way:

Add an AnimationTree, add a NodeBlendTree and select your animation player. Add the node structure as:

Animation --> TimeScale --> Seek --> Output

In the AnimationTree script make method along these lines:

var playing_segment = -1

func play_segment(start):
    if playing_segment == -1:
        set("parameters/Seek/seek_position", start)
        set("parameters/TimeScale/scale", 1)
        playing_segment = start

Then to stop the animation add a Call Method Track in your animation player calling a stop_segment() method in your animation tree. You can use the playing_segment value to add some logic here and make sure the right animation stops in the right place depending on which segment is playing. Remember to reset it to -1 when you’re done.

Here are the docs for animation_tree if you’re not familiar with it:

This is really unnecessary. A Call Method track by itself would suffice.

Rodeo | 2022-01-18 13:25

My suggestion did use a Call Method Track in exactly the way you suggested.

I also added the AnimationTree to have the additional functionality of being able to start as well as stop the animation at any point, something I thought the OP might find useful.

DaddyMonster | 2022-01-18 14:38

AnimationPlayer already has methods advance() and seek() and a property current_animation_position. You could acheive the same thing without the added complexity.

Rodeo | 2022-01-18 20:46

Making a basic animation tree is not complex, it literally takes a few seconds and allows for easy and convenient control which is infinitely more scalable and manageable than relying on an animation player alone.

Regardless, I prefaced my suggestion with Not certain that this is the optimal method but it could certainly be done this way:

If you believe you have a better approach then perhaps next time just leave your own suggestion on a separate thread and let the OP decide which approach they prefer rather than sharing your opinion on a perfectly valid approach from another user. And maybe also check that what you are suggesting hasn’t already been covered.

Let’s leave this here.

DaddyMonster | 2022-01-18 21:18

:bust_in_silhouette: Reply From: Rodeo

The easiest way would be to add a Call Method track to the animation which can simply call the stop() method. Add the keyframe at the time you want the animation to stop at.

If you need to sometimes play the whole animation, make a custom method to hold the logic and use the Call Method track to call your custom method instead.