How to get the exact time it takes to reach a specific animation key frame?

Goal: the cast time of a spell should reflect exactly how long it takes to get to the release frame of the cast/release animation. Changing the cast time of a spell should not require editing existing animations.

Hi all, I am currently manually calculating how long it takes to reach a specific key frame of an animation. What I want to know is if I can do this using information obtained from the scripting API i.e. by checking the AnimationEvent object.

Let’s say we have 5 “cast” animations set up in an animator as follows:

The Casting animation is set to loop - it will only transition into the next animation when a PrepareCast event is fired (using pub/sub).

Each Release animation has it’s own animation event that causes the spell to be cast. I need to transition between the Casting and Release animations based on the cast time of the spell, which could be 2 seconds or even 10 seconds, but also based on the exact time it takes to reach the “cast” key frame of the Release animation (which is different for each release).

The formula I’m using (no background in maths, be gentle!) is: ((F / FPS) / F) * N where F is the number of fames in the animation, FPS is the frames per second, and N is the desired frame we want.

Given an animation that is 25 frames long that plays at 30FPS, say we want to know how long it takes to get to the 8th frame, we use: ((25 / 30) / 25) * 8 = 0.267

Since I know it takes 0.267 seconds to get to the release frame of Release (Hand), I know to trigger the transition between Casting and Release (Hand) when: (currentCastTime - castTime) <= 0.267

For my game it’s important that the cast time of a spell really does reflect how long it takes to cast that spell, but I would also like the spell to be cast at a point in the animation that makes sense (keeps the players immersed, looks much better, feels consistent).

Hi, not really sure what your problem are but the formula could at least be simplified a bit:
((F/FPS) / F) * N => N / FPS

1 Like

You are wildly overcomplicating things for yourself. Just use animation events. Fire the event on the frame of animation you care about:

I understand the value of animation events - the primary purpose of them is to call some method at a specific frame.

The problem is that I want to transition from Animation A (Casting) to Animation B (Release) in a way that guarantees we reach Frame 8 of Release after N seconds has passed (N being the cast time of the spell). Animation events don’t help with that (at least, not to my knowledge). But thanks for the reply!

Derp. Of course :sweat_smile: it’s always a fraction of the FPS value. Thanks.

Isn’t it just to delay the start of the animation, if the animation(s) always should play at the same speed?

Perhaps it also has to be divided in two animations. One for the casting, which is played at the same time the audio clip starts. And one for the release, which is delayed by the audioClipTime - releaseClipTime, to make them always finish at the same time. Just remove the transition arrows between the cast, and release clips so the Animator don’t run them in sequence.

I think I didn’t explain the problem very well in the original post. I don’t have any issues with audio or with animations being “out of sync”. The casting animations are already split into parts.

As you already suggested - by splitting the animation into phases (a generic “casting” animation that loops until it is told to transition into the actual “release the spell” animation), I work around this problem. Now, instead of editing the casting animation, I simply ensure that Casting loops for 0.5 seconds longer than it used to, and I can patch and tweak the timing of spells in the game without touching or editing animations.

That part is already solved. You actually helped me solve the challenge by simplifying the formula - if I know I want the spell cast on frame 8 I can simply do 8 / 30 and I’ve got the amount of time it takes to transition into the next animation. Good stuff!