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
0
Question by GTZerkon · Jun 25, 2015 at 12:53 PM · c#animationscript.

How to enable an animation then disable after it is done playing no matter what fps you are running?

Hey, I could not find any other things relating to this. For some reason, getcomponent().Play("Recoil"); will not work and i have to have it looping. I have it enable it once the gun fires then it plays the animation then disables the animation. I do not understand how to turn it off once it finishes the animation, because different fps will have different animation play times. the clip is .4 seconds long so when i invoke the turn it off aft .4 seconds it sometimes stops in the middle of the animation. I have no idea why it does not let me do the .play for the animator, but it doesn't. here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class GunRecoil : MonoBehaviour {
     public GameObject Gun;
     void AnimationTurnOff ()
     {
         Gun.GetComponent<Animator> ().enabled = false;
     }
     void Update () {
     if (Input.GetMouseButtonDown (0)) {
         
             Gun.GetComponent<Animator>().enabled = true;
             Invoke ("AnimationTurnOff", .4f);
         }
     }
 }

Do you see anything wrong or know how to fix the problem? Any help on why .play() doesn't work???

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

1 Reply

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

Answer by felixpk · Jun 25, 2015 at 02:33 PM

If you really just want to Play one animation I recommend you to use the "old animation system".

  1. Create your animation like you have before

  2. remove the Animator

  3. add an Animation component

  4. click on your animation, turn on debug mode

  5. mark animation as legacy

Now you can access your animation like this:

 if(Input.GetMouseButtonDown(0)){
    Gun.GetComponent<Animation>().Play("Recoil");
 }

You also can stop the Animation like this:

 if(Input.GetMouseButtonDown(0)){
    Gun.GetComponent<Animation>().Stop("Recoil");
 }

When you want it to loop constantly make sure looping in the inspector is enabled! If not you would choose "Clamp Forever", because somehow "once" does not work.

Comment
Add comment · Show 3 · 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 GTZerkon · Jun 25, 2015 at 03:08 PM 1
Share

Thanks, this worked a lot better than what I had. marked as correct.

avatar image felixpk · Jun 25, 2015 at 03:44 PM 0
Share

glad I could help!

avatar image roger0 · Oct 30, 2018 at 09:56 PM 1
Share

How would you do this without the legacy animation system? They will eventually make legacy systems obsolete and then you wont be able to upgrade your project without everything breaking.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i use the Animator in script to find when animation clip finished playing and then stopping the animation ? 1 Answer

Play Animation several times 0 Answers

Restore game objects after animation 1 Answer

Multiple Cars not working 1 Answer

I'm still not get it how to mix/blend soldier character weapon aiming animation with walking animation ? 1 Answer


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