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 termovent · Aug 03, 2013 at 06:31 AM · animationupdatealgorithminterval

Problem updating animation with intervals

Hello guys, first question over here, i'm really new with unity(1 and 1/2 weeks), i picked it up because i need to make a mobile app for uni.

So my question is the following, i have this animation (basically it's a scale and color alpha change), but i only want to run in within a timed interval, i cant put my head arround it, i'm sure i'm missing something easy.

Please check it out! Thanks in advance, Roger

 public float alphaStart;
 public float alphaEnd;
 public float duration;
 public float interval;
 public float targetscaleMul;
 public float force;
 public float forceradius;
 private float start;
 private float actual;
 private bool active;
 private float lerp;
 private Vector3 originalLocalScale=Vector3.zero;
 private Vector3 originalLocalScaleaux=Vector3.zero;
 private Vector3 targetLocalScale=Vector3.zero;
 private float alphaaux;
 private Color newc;
     void Start () {
     start =Time.time;
     actual =Time.time;
 
     active=false;
     originalLocalScale=transform.localScale;
     targetLocalScale=transform.localScale;
     targetLocalScale.y=originalLocalScale.y*targetscaleMul;
     newc=renderer.material.color;
 }
     void Update () {
         actual=Time.time;
         if(active){
             fazanim();
         }else{
             if(actual>=(start+interval)){
                 fazanim ();
             }
                 
         }
     private void fazanim(){
         
         if(!active){
             active=true;
             start=Time.time;
         }
             
         if(active){
             if((actual-start)>=duration*2){
                 newc.a=alphaStart;
                 renderer.material.SetColor("_Color",newc);
                 transform.localScale = originalLocalScale;
                     
                 start=Time.time;
                 active=false;
                 return;
             }    
             
             lerp = Mathf.PingPong(actual,duration)/duration;
             alphaaux=Mathf.Lerp(alphaStart,alphaEnd,lerp);
             newc.a=alphaaux;
             renderer.material.SetColor("_Color",newc);
             transform.localScale = Vector3.Lerp(originalLocalScale, targetLocalScale,lerp);
             
             }
             
         }

what i intended with the interval float is to space out the anim. ex : wait interval. do animation.wait interval . do animation. wait..

but this implementation is basicly skipping the anination the interval ammount

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 TutiBueno2 · Aug 03, 2013 at 06:09 PM 0
Share

Have you tried to replace your animation script to something simple like Sin? i.e:

 alphaaux=$$anonymous$$athf.Sin(Time.time);
          newc.a=alphaaux;
          renderer.material.SetColor("_Color",newc);

2 Replies

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

Answer by Lukas_GMC · Aug 03, 2013 at 06:35 PM

First off, your update code is a waste. The logic you have in there is:

 if(something) 
     runA(); 
 else if(somethingElse) 
     runA();

Why not simply runA? By that I mean get rid of runA and move the code to update.

Also in my experience you're doing timers in a rather unconventional way. I've seen them mostly as:

 float TIMER_DURATION = 2.0f; //Two second timer
 float m_timer = 0f; //Countdown timer
 
 //... somewhere in update
 if(m_timer <= 0f)
 {
     //timer is done. do timer ended logic.
     //and then reset your timer.
     m_timer = TIMER_DURATION;
 }
 m_timer -= Time.deltaTime;
 

You're code is more complex than it needs to be. Simplify it and put some Debug.Logs in various places to verify what numbers you're getting. I would start with Debug.Log(lerp);. I also would put some around to make sure things are getting called in the correct order. Debug.Log("starting anim"); Debug.Log("stopping anim"); Debug.Log("animating");

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 termovent · Aug 03, 2013 at 09:40 AM

tryed with a coroutine, same results, what am i not seeing???

 void Update () {
     actual=Time.time;
     if(actual>=(start+interval) && !active){
         StartCoroutine(fazanim ());
     }
     
 }
 
 
 
 private IEnumerator fazanim(){
         
             if(!active){
                 active=true;
                 start=Time.time;
             }
             
             if(active){
             while(true){
                 if((actual-start)>=duration*2){
                 //yield return new WaitForEndOfFrame();
                     newc.a=alphaStart;
                     renderer.material.SetColor("_Color",newc);
                     transform.localScale = originalLocalScale;
                     
                     start=Time.time;
                     active=false;
                     
                 }    
                 yield return new WaitForEndOfFrame();
                 lerp = Mathf.PingPong(actual,duration)/duration;
                 alphaaux=Mathf.Lerp(alphaStart,alphaEnd,lerp);
                 newc.a=alphaaux;
                 renderer.material.SetColor("_Color",newc);
                 transform.localScale = Vector3.Lerp(originalLocalScale, targetLocalScale,lerp);
             }
             }
             
         }
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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Do I need Inverse Kinematics? Asking for guidelines to program my own custom script 0 Answers

Problem in reloading animation at interval time 0 Answers

Can I make animations snap to a frame? 1 Answer

Animations lose their links upon updating from Asset Server -2 Answers

solution for long simple 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