- Home /
Wait for animation to finish
I have a animated rifle that I am trying to script into playing correctly. The script I have wrote tells the animation to play if the LMB is clicked, this works but I can rapidly click the button without waiting for the animation to finish. How could I make my gun only play the animation when it is not being played to start with?
This is part of my script:
function Update (){
if(Input.GetButtonDown("Fire1")){
Shoot();
}
function Shoot () {
if(Input.GetButtonDown("Fire1")){
if(bulletsLeft > 0 ){
bulletsLeft -- ;
animation.Stop();
animation.Play("shoot");
}
}
}
Answer by Xroft666 · Oct 23, 2012 at 12:52 AM
Updated 28.10 :) Search for differences :)
function Start () {
animation["reload"].speed = 1.8;
animation["shoot1"].speed = 2.6;
animation["shoot1"].wrapMode = WrapMode.Once;
animation["reload"].wrapMode = WrapMode.Once;
animation["shoot1"].layer = 1;
animation["reload"].layer = 2;
animation.Stop();
}
var ableToShoot = true;
function Update ()
{
if(Input.GetButtonDown("Fire1") && ableToShoot )
Shoot();
if(Input.GetKeyDown("r"))
Reload();
}
var fullClip : int = 8;
var bulletsLeft : int = 8;
var waitTime = 1.8;
function Shoot ()
{
if(bulletsLeft > 0 )
{
ableToShoot = false;
bulletsLeft -- ;
animation.Stop();
animation.Play("shoot1");
}
}
function OnComplete() { ableToShoot = true; }
function Reload ()
{
ableToShoot = false;
animation.CrossFade("reload");
yield WaitForSeconds(waitTime);
bulletsLeft = fullClip ;
ableToShoot = true;
}
It doesn't play the animation at all now but I don't have any errors.
Your object probably has some kind of looped idle animation. Does it? To check it turn off the "Play Automatically" flag in Animation component. It must be it.
No problems. Step by step. Slowly and solid :)
Here is your modified script. I updated it in head answer
In Unity open "Animation" windows. You can find it in "Window" tab.
Select your animated object and then you need to select the animation we want to edit - "shoot". You can pick it in the left top corner. On screenshots there is a label "Turn180Degree". http://docs.unity3d.com/Documentation/Components/animeditor-AnimationEvents.html In your case it must be "shoot"
Pick the last frame in your animation and as you can see on the screenshots you need click under timeline with your right button and create an Animation Event. There you will be able to choose which action you want to trigger when animation ends. There must appear "OnComplete" name. You choose him.
And that is all. I hoooooope it will help :)
Answer by spiceboy9994 · Oct 26, 2012 at 11:15 PM
Here's another way to do this. This is the one I use for my game:
GameObject objectToAnimate;
void Start()
{
objectToAnimate = GameObject.Find("objectName"); // Just in case is an external object
}
void StartAndWaitAnimation(string animationName)
{
StartCoroutine(PlayAnimationAsync(animationName));
}
IEnumerator PlayAnimationAsync(string animationName)
{
objectToAnimate.animation[animationName].wrapMode = WrapMode.Once; // choose your wrapping mode
objectToAnimate.animation.Play();
yield return new WaitForSeconds(objetToAnimate.animation[animationName].lenght); //This does the magic
//Do the stuff after the animation ends
DoOtherStuff();
}
This way you separate your code from your animation design, so the code takes care of the order and execution, instead having lots of triggers on your animations. Just a point of view.
Hope this helps.
does not work for me :-( I get the code still firing BEFORE the animation completes?
Answer by sfc.itzhak · Oct 25, 2012 at 10:48 PM
http://docs.unity3d.com/Documentation/ScriptReference/Animation.IsPlaying.html
function Shoot () { if(bulletsLeft > 0 && !animation.IsPlaying("shoot") ){ ableToShoot = false; bulletsLeft -- ; animation.Stop(); animation.Play("shoot"); } }
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animator.GetCurrentAnimatorStateInfo(0).IsName("FallOffBed") unexpectedly returns false 0 Answers
Play animation once 1 Answer
Play animation for horizontal and vertical movement? 1 Answer
Mixamo Call Animation from Script 0 Answers