- Home /
How can I get the legnth of an animation? (Mechanim Animator/C#)
I'm making a First Person Shooter, and I'm currently trying to get my gun to reload, but I want to make the game wait until the reload animation has finished before the player can shoot again, I'm having trouble with this.
I am using the Mechanim Animator to do my animations, but all the solutions I see use the regular animation component.
I would think that you use "WaitForSeconds", the problem is, I don't have the value for the WaitForSeconds to wait for.
So how would I get the legnth of an animation when I'm using the mechanim animator? Preferably in C#.
Answer by Lovrenc · Jan 01, 2013 at 11:45 PM
I dont know wheather you can get animation lenght, but you dont really need to know it. You should have different animation/gameplay states. Eg. (ready, reloading, jumping,...).
static int reloading = Animator.StringToHash("Base.Reloading");
//Base - layer name; Reloading - animator state name
Then based on your animator state you decide wheather you can shoot or not. Animator will make shure your state gets to ready after reloading animation is finnished (exit-time).
I would weapon animations on separate layer than movement etc.
Animator anim;
void Start (){
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
currentBaseState = anim.GetCurrentAnimatorStateInfo(0); //if your stuff is on 1st layer
if (currentBaseState.nameHash == reloading){
//CANNOT SHOOT NOW
}
}
Yeah but the problem is with the way I coded it, I need to know the legnth to tell the game how long to keep the realoding boolean as true.
Sorry, i dont know about that nor was i able to find solution on the web.
That's okay, I have made a temporary solution, thanks anyway.
Answer by Ouss · Oct 20, 2013 at 12:17 PM
Hello, to get the length of the current state: yourAnimatorVariable.GetCurrentAnimatorStateInfo(0).length
0 if it the first layer, 1 if it's the second, etc
Your answer
Follow this Question
Related Questions
Mecanim animation 1 Answer
Controlling value of blend parameter via script 0 Answers
Animation component in Unity 4.3 doesn't work? 2 Answers
Distribute terrain in zones 3 Answers
Running animation 1 Answer