How can I get current animation frame

Unity3d web site is weird. When I search, Gateway out occurred on web T.T

Anyway, I make FPS game.

I want enemy shoot with right animation frame.

So I guess, If I know current animation frame, I may script that frame to shoot bullet.

Site is temporarily having some issues, I have the same problems with searching. You should use google, preceding your question with unity3d/unity3d answers/unity3d forums. (Will probably help you faster than waiting for an answer.)

There is not enough information to properly answer the question. How are you doing animations right now? What do you mean by right animation frame? The right animation, or the right animation time? (there are no animation frames in unity) Why would animations dictate when to shoot? Shouldn't the script that does the animation logic tell you when to shoot?

When enemy shoot something, animation runs like aiming -> shooting. then I shoot bullet in shooting motion not aiming motion. So as I think, I should know what is the current frame of animation.

@BenProductions1 , animations DO have frames, animation.time is just a friendlier way of saying current frame...

@Seth_Bergman No they don't, animations are curves. Curves between key-frames, which really never get directly applied. The result of the animation getting applied is then a state, not a frame... animation.time is not a way of saying current frame, but a way of saying how far along the animation along it's curves is. GOD damn technicalities :P

4 Answers

4

Perhaps use Animation Events?

This saved me, thank you so much. Don't know how ugly is this, but I needed to move by pixels a pixel perfect player exactly on some frames of walk animation, and this helped me. (don't have enough rep to vote up)

This should be the best answer!

The best way I know is with AnimationState.normalizedTime

this way 0 is the first frame, 1 is the last, .5 is the middle frame… IF you know how many frames there are, this can lead you to the right number…

if(animation["Aim"].normalizedTime >= .9)
animation.Play("Shoot");

to answer the question,but this may not be the best for what you actually want…

It is absolutely ridiculous that 15 YEARS after Unity’s creation, no developer thought it might be useful for a user to get the current frame of an animation clip.

Okay, so a good technique to follow up to the current answer…

For me I was working with 2D, and the way I used events was to create a 2DCharacterGraphics.cs script that I attached to every one of my 2D characters. I created a method like so:

   //This is the 2DCharacterGraphics script. It has this declared and works with it: 
   private animationID;

public SetAnimationID(int ID) { 
  animationID = ID;
}

switch(animationID) { 
case 0:
//edit sh*t for animation ID 0
     break;
  case 1:
    //edit sh*t for animation ID 1
     break;

 }

Then the animator SHOULD show your method (since it’s public) if you select an event to add. For the event, you just type in your ID and you’re done.

You can now make all kinds of sh*t happen in C# within your state machine above.