- Home /
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
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);
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");
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);
}
}
}
Your answer
Follow this Question
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