- Home /
[SOLVED] start Animation with whatever value is the current one
Hey everyone, I'm trying to animate the color of an object.
It's supposed to change based on certain key presses.
I want to start the animation with whatever color is the current color of the object and transition to the new desired color. Then set this color permanently until another animation or function changes it.
The problem is that the animation always takes the color value that the object had at the moment I was creating the animation/keyframe and that the color reverts to the original color of the prefab (which I set to white) after the animation is finished.
Additionally I can't even at least make the change to be permanent by making an event at the end of the animation and setting the new color value via script, even though the function in the script gets called. It also always reverts to the prefabs original color.
why dont use color.Lerp? u can put this on a coroutine, a rough example, using fixedDeltaTime:
public Color StartColor;//if you want to have a starting fixed color, well not now so ignore this line
public Color FinalColor;
public float Delay; //delay seconds between the coroutine call and start changing
public float Duration;//duration of the transition between colors
public $$anonymous$$aterial; //$$anonymous$$aterial of the object
IEnumerator CC(){
Imagen.color = StartColor;//ignore this too, set a fixed color on start
yield return new WaitForSeconds (Delay);
float Step = 0;
while (Step<1.00001) {
Step += Time.fixedDeltaTime / Duration;
$$anonymous$$aterial.color = Color.Lerp (StartColor, FinalColor, Step);
yield return new WaitForSeconds (Time.fixedDeltaTime);
}
yield return null;
}
after put this on the script, replace where you use animation.Play()
use a
StartCoroutine (CC ());
@DCordoba I wish I would've seen your reply earlier but this is what I did and it worked now :D I'm so happy! But still it would be nice if they implemented something where you can just start with the variables that the Object currently has (as of while the game is running) because now it is hard to time the duration according to the animations speed (if e.g. i change the animations speed for different situations)
EDIT: It works as intended but I like to fully understand what I write. Right now I don't understand why we need to do this: yield return new WaitForSeconds (Time.fixedDeltaTime);
could you explain that to me please?
Well, the coroutine is similar to a thread, ("similar" in purpose and behaviour) the ienumerators could be seen like the list of execution of the thread.
yield return
gives a returning result (as return
do) but keeps executing inside the "tread" so the execution don't abandone the ienumerator.
WaitForSeconds indicate to the coroutine to stop execution for the following amount of time, similar to wait(); on the threads...
And Time.fixedDeltaTime is the time to the physics are uptaded (it is fixed on execution, difference with DeltaTime, who depends of frames per second) and can be changed on inspector options.
So with this instruction we are saying to the coroutine, ok, lets wait the next fixed amount of time, before continue changing the color, it make the coroutine have a finite, equals, steps to make the slow transition, the number of steps are given by Duration/Time.fixedDeltaTime
Edit To Clarify: the WaitForSeconds just stops the current coroutine execution, (this thread? execution) the rest of "independent" tasks (scripts outside the Ienumerator) continue as usual, without being affected.
Please not confuse it with the inverse Time.fixedDeltaTime/Duration
, this gives the percentage (scaled from 0 to 1) completed on the current step, the sum are used to get the total percentage completed, and given to Lerp to calculate the color on this step.
Well, you will see each steps slow or fast depending of the fps at the moment, but will take the same amount of time (duration) in "real" seconds.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Handling Build-up of Root Motion Errors Over Time 2 Answers
Animate an Object by letting it play out in when I press play? 0 Answers
My player won't jump? 1 Answer
Animation stops rotation 0 Answers