• Runtimes
  • [Unity] Problem mixing tracks

  • Düzenlendi
Related Discussions
...

Hi!

I'm desperately trying to combine full body animations with arms animations for shooting, magic, etc.

My character is playing full body animations (idle, walk, run...) on track 0.
Arm animations are played on track 1.

To be able to blend full body and arm animations I use a little trick :
Say, while track 0 is playing the run animation, if I want to make my character shoot while running, I play an empty animation on track 1 and then the real shoot animation.

animationState.SetAnimation(1, emptyAnimation, false);
animationState.SetAnimation(1, shootAnimation, false);

That way, the arm is correctly interpolated from the current position (depending of the full body animation) towards the shoot animation.

But when the shoot animation is complete I can't get to bring back the arm to the position it should be in the full body animation. with the same blending. But the arm returns to position immediately without any interpolation.

How can I achieve that?

In not sure what the empty animation is doing for you? I don't think it does anything. Tracks are applied one after another, lowest to highest, and overwrite the pose of previously applied tracks. You can set a mix property on the track entry returned by add/setAnimation to control how much of the track below is mixed. It sounds like you want to transition the track mix from 0 to 1 over a short duration at the star of your shoot animation, then from 1 to 0 at then end of it. AnimationState doesn't currently have a way to do this transition for you, though it may be a nice feature.

Actually, setting a mix of 0.1 from the empty to the shoot animation allows me to transition from the track 0 to 1. Without this empty animation, it would play the shoot animation without interpolating from the current position of the arm (on track 0). It works in one direction (from 0 to 1), but I can't do the same in the other way (return from 1 to 0).

I'll try to play with the mix property as you said, but having a way to transition from one track entry to another would indeed be a nice feature I'd like to see in a future version of AnimationState! 😉

EDIT: I may have found a workaround to achieve what I need... It works, but to get things perfectly in sync, I need to know the current frame being played by my animation. Is it possible to know that? And is it possible also to play an animation and start it at a specific timeframe?

You can keep a reference to the TrackEntry that SetAnimation returns. Or use AnimationState.GetCurrent(trackIndex) to get the TrackEntry.
A TrackEntry has a float field called time. You can use that to calculate the "frame number".

"Frame number" would be TrackEntry.time * 30f, since there are 30 frames in one second in the editor.

Likewise, you can play an animation starting at a certain point by calling SetAnimation and then immediately changing the time of the TrackEntry it returns to the time you want to start at. (I'm sure you can do the math to calculate the time based on frame number. 😉 )

Pharan yazdı

You can keep a reference to the TrackEntry that SetAnimation returns. Or use AnimationState.GetCurrent(trackIndex) to get the TrackEntry.
A TrackEntry has a float field called time. You can use that to calculate the "frame number".

"Frame number" would be TrackEntry.time * 30f, since there are 30 frames in one second in the editor.

Likewise, you can play an animation starting at a certain point by calling SetAnimation and then immediately changing the time of the TrackEntry it returns to the time you want to start at. (I'm sure you can do the math to calculate the time based on frame number. 😉 )

Doh! I feel so stupid. 🙂 I should have inspect a bit more the TrackEntry class...
So, a big thank you, Pharan, for this information. It's gonna help me greatly!
And "yes", I should be able to do the required math! 🙂

3 ay sonra

Sorry to dredge up an old post, but I'm having the same problem. Was this ever solved?