x


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

more ▼

asked Aug 03 '13 at 06:31 AM

termovent gravatar image

termovent
3 1 1 2

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

alphaaux=Mathf.Sin(Time.time);
         newc.a=alphaaux;
         renderer.material.SetColor("_Color",newc);
Aug 03 '13 at 06:09 PM TutiBueno2
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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");

more ▼

answered Aug 03 '13 at 06:35 PM

Lukas_GMC gravatar image

Lukas_GMC
38 1 6 8

(comments are locked)
10|3000 characters needed characters left

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);
         }
         }

       }
more ▼

answered Aug 03 '13 at 09:40 AM

termovent gravatar image

termovent
3 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5702
x694
x72
x20

asked: Aug 03 '13 at 06:31 AM

Seen: 727 times

Last Updated: Aug 03 '13 at 06:35 PM