Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 birns92 · Apr 18, 2016 at 02:04 AM · animationbuttonspause

Pause an animation with a button

I have animated my character upon button press to touch the floor.

alt text

I want to be able to pause the animation at any given time with either the same button or a different button. Here is the code for my button to play the animation.

 public class Touch_floor_1 : MonoBehaviour {
 
     Animator animator;    
     void Start () {
         animator = GetComponent<Animator>();
     }    
     public void Touch ()
     {
         animator.SetTrigger("Touch");        
     }    
 }

I have read a few articles on unity, specifically this one, yet I am unsure of how to go about doing this.

unity-question.png (436.0 kB)
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
1

Answer by Addyarb · Apr 18, 2016 at 02:24 AM

Greetings,

The easiest way I can think of is to set animator.speed to zero.

If this doesn't work for you, please comment below and I'll give you a range of other solutions based on what you need!

Your app looks awesome, keep up the good work. Good luck!

EDIT:

Here is a piece of code that (hopefully, I haven't tested yet!) sets the current state's animation speed to zero. It does this by getting ALL of the Animator's states, iterating (in Layman's terms, go through each of them) through them, and then seeing if one has the same name as the one that is currently active. If it does indeed have the same name, it references it and then sets it's speed to zero.

You can use this same method to un-pause it (just set the speed to 1).

Why is this better or different than just setting Animator.speed to zero? Well, you may have multiple layers in your Animator, and settings the entire Animator's speed to zero may cause some issues.

Anyhow, here's the code!:

 foreach(AnimationState state in anim)
             {
             if(anim.GetCurrentAnimatorStateInfo().fullPathHash == state.name)
                 {
                 state.speed = 0;
                 }
             }
         }

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 birns92 · Apr 18, 2016 at 07:31 AM 0
Share

Thanks for the response!

I ended up using this code:

 public Animation anim;
     public void Pause() {
         anim = GetComponent<Animation> ();
         foreach (AnimationState state in anim) {
             state.speed = 0F;

When I use the one you provided, I receive an error that "Animator" does not contain a definition for "GetCurrentAnimatorStateInfo" and no extension method "GetCurrentAnimatorStateInfo" of type `Animator' could be found. I am missing a using directive or an assembly reference, but I am not sure what.

avatar image MagoParodi · Aug 23, 2018 at 09:59 AM 0
Share

$$anonymous$$aybe not the classiest, but worked like a charm! Thanks!

Always remember to set it to 1 again when you need the animation to play again.

Thanks!

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

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

Related Questions

What's a good book to learn scripts for Unity? 1 Answer

Pausing an animation 3 Answers

Can I make an animation while game is paused? 3 Answers

How do I pause everything in the scene until the animation countdown prefabs done counting down in the scene 3 Answers

How do I combine a GUI scroll with an 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