- Home /
Quick change over time math
Hello, all! I am writing a script that allows you to set parameters for the time you would like a guitexture to stay dormant, then fade in, then stay constant, then fade out.
I would just like to know how to calculate this:
After the number of dormant seconds has been reached, I would like to calculate the rate at which an object needs to fade in and how to make a fluid transition independent of frames.
For instance, if the FadeinTime variable is set to 4.0 seconds, I would like to take the guitexture's alpha from 0 to 100 in 4.0 seconds, but instead of updating every second, I'd like to update it as quickly as possible at a set rate.
How would I do this?
Thanks for the help! (And I will release the script when I finish) - YA
Answer by Bovine · Jul 18, 2012 at 09:15 PM
You should Lerp the colour in Update().
See Unity's example here:
http://docs.unity3d.com/Documentation/ScriptReference/Color.Lerp.html
You will need to provide your own time value based on simple maths for how long has elapsed since the fade began.
Note you could also do this with a Coroutine, which is probably a better choice, so you don't Update() unnecessarily:
http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
Quick question though: How do I use the 't' field in the $$anonymous$$athf.Lerp? If I want it to go from an alpha of zero to an alpha of 1 in 3 seconds, I cannot type in alpha = $$anonymous$$athf.Lerp(0.0,1.0,3); That does not work...
T is normalised time so you need to say from 0 to 1 how far through the Lerp operation you are. This would be best done in a coroutine if it is a one shot kind of thing.
Hmm... I already have it in a coroutine. So what value would I insert for 3 seconds? 0.3333?
Something like:
IEnumerator Fade(material mat, float duration, Color start, Color end) { // remember the start float start = Time.time; do { // calculate how far through we are float elapsed = Time.time - start; float normalisedTime = $$anonymous$$athf.Clamp(elapsed / duration, 0, 1); mat.color = Color.Lerp(start, end, normalisedTime); // wait for the next frame yield return null; } while(elapsed < duration); }
I've not compiled it, but it should be fine.
Answer by Bovine · Jul 20, 2012 at 10:01 AM
Something like:
IEnumerator Fade(material mat, float duration, Color start, Color end)
{ // remember the start
float start = Time.time;
do
{ // calculate how far through we are
float elapsed = Time.time - start;
float normalisedTime = Mathf.Clamp(elapsed / duration, 0, 1);
mat.color = Color.Lerp(start, end, normalisedTime);
// wait for the next frame
yield return null;
}
while(elapsed < duration);
}
I've not compiled it, but it should be fine.
Thank you so much. I have been coding for a while but I guess I'm really not as proficient at this as I thought I was xD Alright just two more questions: What is IEnumerator? Are you just passing in variables? And what is the while statement for? And is "do" actually necessary for a coroutine? I'm sorry that I am bombarding you with questions but I'd like to pick something up from this rather than just copy-paste code. Thanks so much! -YA
Good grief! There are a raft of questions ranging from relatively simple program$$anonymous$$g issues to quite big ones.
Coroutines in Unity allow you to yield execution of the next part of the coroutine until some future time - the next frame, fixed update time or a set period of time, via WaitForSeconds().
This mean that once you StartCoroutine() that your Coroutine will run until you yield and will potentially be run again the following frame and so on until you yield break or the coroutine returns (you must yield at least once for some reason).
Coroutines in Unity are documented here:
http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
IEnumerator is an enumerator - the coroutine is treated as an enumeration, but this is typically not something you will care about.
Enumerators are described here however:
http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx
do, while is a general program$$anonymous$$g construct. The syntax may differ from language to language, but in c# you need to begin a do, while with do. A while, do you do not need to end with do - the ter$$anonymous$$ating scope brace will do this for you. This construct does not change just because we are in a coroutine.
You would use the Coroutine above by calling:
StartCoroutine(someRenderer.material, 3, Color.white, Color.black);
Your coroutine would begin, perhaps at that point, I don't know (though I have never checked) and the function would execute normally until it hit the yield return null. At which point the Coroutine would return, technically yield and execution would return to the gameloop which would eventually finish processing that frame.
When the next frame is executed, at some point, because you yield return null, the coroutine will pickup its execution at the instruction following the one it last yielded at. So your routine would check the elapsed time calculate wasn't more or the same as the duration and if it wasn't it would execute from the do again, and yield once more and so on and so forth.
Coroutines are state machines. Ins$$anonymous$$d of you having to remember what you were doing using some sort of switch statement or series of nested switch statements, the framework takes care of it for you.
$$anonymous$$ore detail can be found in this answer:
http://answers.unity3d.com/questions/36690/mechanics-of-coroutines.html
I can't rally help much more, but do search the internet and read around the topics mentioned.
Note that you can also fade a material's color using an animation.
Oh yes I am not that much of a newbie to know about animation haha. While I have worked with 'while' statements, never have I worked with a do-while. Doesn't sound all too complicated. Thank you very much this clears up a lot. Very glad you found this thread haha. Thanks for the help! -YA
Oh my gosh I really do apologize but I am working in Javascript not C#... And it appears IEnumerator interface is C#...?
Read the docs please, there are equivalents of all of the above in JS and the docs contain examples. I've written the routine for you in C# - I can't really give you anymore help.
Your answer
Follow this Question
Related Questions
How Do I Fix Unexpected Char 0x201C? (Code Below) 1 Answer
i need some code suggestions 1 Answer
Wall Jump script 2 Answers
AssetBundle Android 1 Answer
Error BCE0018 0 Answers