• 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 akajb84 · Jul 27, 2012 at 08:14 PM · animationpingpong

Play animation forward than immediately backwards, once (ie single pingpong)

Is there a way to play a single ping pong of an animation. I have an animation of a hand reaching out. Using pingpong, it reaches forwards and then back, which is what I want. But I only want it to happen once, not continually loop forever.

Is there an easier/better way to do this than by playing it once, and once it reaches the end, playing it again backwards?

Comment
Add comment · Show 1
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 akajb84 · Jul 27, 2012 at 08:17 PM 0
Share

I've currently set it to pingpong, and am watching until time gets > than length * 2. But this seems like a stupid way to do it...

2 Replies

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

Answer by Seth-Bergman · Jul 27, 2012 at 08:22 PM

once you start the animation, you can check it using animation.normalizedTime:

 function Grab(){
 animation["grab"].Play();
 while(animation["grab"].normalizedTime < .1)
 yield; //just making sure anim has started, there may be a prettier way to do this, but it'll work
 if(animation["grab"].normalizedTime == 0) //back to the first frame
 animation["grab"].Stop();
 }

normalizedTime is a float, from 0 (the first frame) to 1 (the last frame)

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 akajb84 · Jul 27, 2012 at 08:29 PM 0
Share

I wonder if you'd actually be able to catch it at normalizedTime == 0, considering it'll immediately start counting up again (as it starts into the next iteration).

But I didn't know about normalizedTime before, so that's good to note.

avatar image Seth-Bergman · Jul 27, 2012 at 08:34 PM 0
Share

when it hits that frame, it should register I'm sure, but I can't say 100%, haven't actually tested this

avatar image KevinCodes4Food · Apr 23, 2014 at 08:30 PM 0
Share

I hit situations where Stop was not called using fast or short animations. See my notes and updated solution below.

avatar image
0

Answer by KevinCodes4Food · Apr 23, 2014 at 08:28 PM

Thank you, Seth, for your idea to use animation timing to control how many times the anim plays. Unfortunately, though, I encountered situations where the code set above never triggered the stop, specifically with fast or short animations.

I implemented a simplified version of Seth's concept to stop more reliably and precisely using 2X the duration of the animation. Here it is in C#:

     // Play, crossfade, or blend into your animation here 
     animation.CrossFade(animationName);    
     StartCoroutine("PingPingOnce", animName);

...

     IEnumerator PingPingOnce(string animationName)
     {
         animation[animationName].wrapMode = WrapMode.PingPong;
     
         // Wait for a forward and backwards animation play
         yield return new WaitForSeconds(animation[animationName].length*2/animation[animationName].speed);
                     
         animation.Stop();
     }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Can I make animations snap to a frame? 1 Answer

How to select an animation clip by index number? 7 Answers

can't find pingpong mode in Unity 4.6? 0 Answers

How do I set a 2D sprite animation to ping-pong wrap mode? Debug mode setting does not work 3 Answers

pingpong animation question 2 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