Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Blink · Oct 22, 2012 at 04:26 PM · animationwaitfinish

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");
 
    }
 
 }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
 }
Comment
Add comment · Show 17 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blink · Oct 23, 2012 at 03:45 PM 0
Share

How would I go about doing this?

avatar image Xroft666 · Oct 23, 2012 at 04:06 PM 0
Share

(update) :)

avatar image Blink · Oct 23, 2012 at 04:10 PM 0
Share

It doesn't play the animation at all now but I don't have any errors.

avatar image Xroft666 · Oct 23, 2012 at 04:28 PM 0
Share

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.

avatar image Xroft666 · Oct 25, 2012 at 09:13 PM 1
Share

No problems. Step by step. Slowly and solid :)

  1. Here is your modified script. I updated it in head answer

  2. In Unity open "Animation" windows. You can find it in "Window" tab.

  3. 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"

  4. 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 :)

Show more comments
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xroft666 · Oct 27, 2012 at 01:18 AM 0
Share

yeah, this one is good as well

avatar image elpuerco63 · Oct 03, 2014 at 11:30 AM 0
Share

does not work for me :-( I get the code still firing BEFORE the animation completes?

avatar image
-1

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"); } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges