- Home /
Determine in which frame is an Animation currently
Hi, the problem that I have encountered is that I want to know in what frame an animation is, since depending on it, when an object is killed a different animation should play so it makes sense. For example, if the enemy is looking up, when its killed it plays a different animation than when he is looking down. I've tried with CurrentAnimatorStateInfo but can't achieve the results I want. I´ve also tried a code found somewhere else, in which it recommends using this:
ratio = GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).normalizedTime / GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length;
frame = ratio * (framesInAnimation);
in order to determine in which frame the animation is, but still I can't determine the current frame of an animation, any other ideas on how to approach this problem?
What I want to achieve is something like this:
public void Death()
{
if(frame == 1)
{
animator.SetBool("Death1", true);
}
if(frame == 2)
{
animator.SetBool("Death2", true);
}
etc...
}
Answer by GiyomuGames · Jul 01, 2015 at 12:44 AM
So you have a long animation during which the enemy is looking down, then up, etc. Something like that?
If so then the best way to do what you want to do is I believe to have 2 (or more) animations instead of 1. You should have 1 animation for each status of your enemy. Assuming your enemy is either looking down or up: you can create a first animation "enemy look down" with the first half of your sprites, and then "enemy look up" for the second half of your sprites. Then in your animator you make them loop (when "enemy look down" is finished it goes immediately to "enemy look up", when "enemy look up" is finished it goes immediately to "enemy look down"). In the game you won't be able to tell that there are actually 2 animations looping. Thanks to this you can then create a transition between each animation to your appropriate death animation ("look down death", "look up death" for example). When your "Death" boolean is true (no need for Death1 or Death2) it will trigger the suitable animation depending on the origin state.
I hope that helps.
Answer by hnmikechan · Jun 30, 2015 at 06:51 PM
Can you enter in an event trigger in the animation clips timeline? You can choose the frames you want for the events. Then you can specify the method to check which death sequence you should play.
http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html
I am not able to edit any of the animations in the Animation Window, it won't allow me to add animations to it. If it affects somehow, I created the animations by dragging multiple sprites into the scene, and the animation file was automatically generated.
The animations were imported from another program like max or blender?
No, they where made like commented before, just drag & dropped the sprites into the scene.
Your answer
Follow this Question
Related Questions
Continuously Play Animation Forwards and Backwards with Mechanim 0 Answers
Animator.GetCurrentAnimatorStateInfo(0).IsName("FallOffBed") unexpectedly returns false 0 Answers
Can't Check If Animatorstate Is In Second Layer 1 Answer
Can an empty state in Animator cause performance issues? 1 Answer
Switch animation state while keeping animation timing 0 Answers