Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Soilyman · Mar 04, 2018 at 04:51 PM · animationcoroutineienumeratoranimationcurve

AnimationCurve as fixed time rather than portion of time?

Sorry for the title gore, but I'm new to using Animation Curves and I wasn't sure how best to weird the issue.

I'm using an AnimationCurve variable to animate the size of a bubble shield type object. On activation, the shield will grow from a scale of 0 to slightly greater than its intended scale and then quickly revert back to its intended size for the duration of the shield, just before the shield has reached its duration the animation will perform in reverse. The best example of what I'm after can be found in Animal Crossing: Pocket Camp, when dialogue is initiated with an NPC the text bubble animates in a similar way; though slightly more subtle.

The problem I'm having is that I want the bubble's opening and closing animation to take the same amount of time regardless of how long the bubble's duration is, but the AnimationCurve works on a scale of 0 to 1. This means that if the curve is set from a time of 0 to 0.1, and the bubble duration is 5, then the opening animation will last 0.5 seconds whereas I might want it to last o.1 seconds regardless of duration.

The curve and coroutine for the animation are as follows:

alt text

     private IEnumerator ActivateShield() {
         m_DurationElapsed = false;
         m_CooldownElapsed = false;
 
         m_ThisTransform.localScale = Vector2.zero;
 
         for (float x = 0f; x < m_ShieldCooldown; x += Time.deltaTime) {
 
             if (x <= m_ShieldDuration) {
                 float y = m_ShieldCurve.Evaluate(x / m_ShieldDuration);
                 m_ThisGameObject.transform.localScale = new Vector2(y, y);
             }
             else if (!m_DurationElapsed) {
                 m_DurationElapsed = true;
                 m_ThisSprite.enabled = false;
                 m_ThisCollider.enabled = false;
             }
 
             yield return null;
         }
 
         m_CooldownElapsed = true;
         m_ThisGameObject.SetActive(false);
     }


bubble.jpg (228.3 kB)
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 RobAnthem · Mar 04, 2018 at 04:57 PM 0
Share

You should make the two parts separate animations, so that you can adjust the animation speed of the specific animation at the appropriate time.

1 Reply

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

Answer by Bunny83 · Mar 05, 2018 at 04:38 PM

Well, like Rob said you want to handle the growing / shrinking seperately from the actual duration. If the collapsing animation should always look exactly like the growing animation you can just create a curve for growth and play it in reverse when collapsing. If you want to allow a seperate grow / collapse animation you can either use two seperate curves or just "split" the curve into two logical parts. For example having the time 0 - 1 represent the growing animation and 1 - 2 the collapsing animation. You can simply do:

 private IEnumerator ActivateShield()
 {
     m_DurationElapsed = false;
     m_CooldownElapsed = false;
     m_ThisTransform.localScale = Vector2.zero;
     Transform trans = m_ThisGameObject.transform;
     // growth
     for (float t = 0f; t <= 1f; t += Time.deltaTime/m_GrowthTime)
     {
         trans.localScale = Vector2.one * m_ShieldCurve.Evaluate(t);
         yield return null;
     }
     
     // duration
     yield return new WaitForSeconds(m_ShieldDuration - m_GrowthTime - m_ShrinkTime);
     
     // shrinking
     for (float t = 1f; t >= 0f; t -= Time.deltaTime/m_ShrinkTime)
     {
         trans.localScale = Vector2.one * m_ShieldCurve.Evaluate(t);
         yield return null;
     }
     
     m_DurationElapsed = true;
     m_ThisSprite.enabled = false;
     m_ThisCollider.enabled = false;
     m_CooldownElapsed = true;
     m_ThisGameObject.SetActive(false);
 }

In this case we just play the same curve "backwards" (from 1 back to 0). Note that the time inside the curve should have a fix range from 0 to 1. The actual speed of the growing / shrinking is controlled by the "m_GrowthTime" and "m_ShrinkTime" variable. For the duration we just wait for the remaining amount of time. Though if you want the animation to be interruptable you can replace the WaitForSeconds also with a loop like the other two.


If you want both animations within the same curve you would replace the last loop with something like this:

     // shrinking
     for (float t = 1f; t <= 2f; t += Time.deltaTime/m_ShrinkTime)
     {
         trans.localScale = Vector2.one * m_ShieldCurve.Evaluate(t);
         yield return null;
     }

So this would just "play" the curve from 1 to 2.

Comment
Add comment · Show 1 · 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 Soilyman · Mar 07, 2018 at 08:47 PM 0
Share

Sorry, been away from my computer for a few days but really appreciate the thorough answer. I guess I expected there to be a property or something I could change rather than having to add a separate curve but this is fine. I've incorporated some of the code you shared and got things working perfectly now.

Thanks again for taking the time out to reply!

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

207 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 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 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

Yield Wait for Seconds (A little help here) 2 Answers

Mecanim animation, playing once 1 Answer

Line of code skips randomly at times? 1 Answer

Play animation then destroy gameOject C# "SOLVED" 1 Answer

WaitUntil and WaitWhile don't work 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