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 $$anonymous$$ · Aug 01, 2014 at 01:09 AM · animation2deditoranimator

Unity2D - How do you restart/interrupt an animation?

Hey everyone,

So, I'm back to Unity and am playing around with the new built-it 2D features. They seem quite wonderful!

Currently, I'm playing around with the 2D animator graph feature where you use the visual finite state machine window to link together animations.

I have this very simple animation tree: alt text

Basically, it's just a tank where I want the turret to move back when you shoot which I've already sorted out. Currently, to transition from idle to shoot, it's simply a bool called "Attack" that I have to set to true. For the "shoot" state to transition back to "idle", it just plays on the exit time condition right now.

As a simple test, I have this logic to start the fire animation when you fire the turret:

         if (Input.GetKeyDown(KeyCode.Space))
         {
             GetComponent<Animator>().SetBool("Attack", true);
         }

Now, the problem is that if I spam the space button, it will still play the entire animation to the end before it wants to replay it. What I want to do is have the ability to "interrupt" the animation if the player fires another bullet in the middle of the current fire animation. In that case, it should simply restart the animation. I haven't been able to figure out the way to do this yet. How would this be done?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Reeceg · Aug 01, 2014 at 01:39 AM

 if (animation.IsPlaying("animation") && Input.GetKeyDown(KeyCode.Space))
        {
            animation["animation"].time = 0;
            yield
            GetComponent<Animator>().SetBool("Attack", true);
        }


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
avatar image
0

Answer by RedSuinit · Aug 01, 2014 at 02:33 AM

Perhaps try adding this line to your code.

  if (Input.GetKeyDown(KeyCode.Space))
         {
             GetComponent<Animator>().SetBool("Attack", false);
             GetComponent<Animator>().SetBool("Attack", true);
         }

This way the variable attack will be false then true immediately afterwards, thus stopping, then restarting the animation.

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 $$anonymous$$ · Aug 01, 2014 at 10:48 AM 0
Share

Unfortunately I already tried this, and it didn't work :(

avatar image RedSuinit · Aug 01, 2014 at 02:00 PM 0
Share

Never $$anonymous$$d I'm a moron who can't read apparently. The issue with my method is that you are using exit time for the condition to end the animation. It looks to me though that Reecegs answer should do what you want.

Or you can add another transition, if you add a transition from any state to idle that is solely based on attack being false that would do what you want with the code that I provided.

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

How to start an animation by the exit animation frame 0 Answers

Animator behaviour on GameObject 1 Answer

Animation won't play more than once 1 Answer

How to use one animation and Animator Controller on multiple objects? [2D] 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