- Home /
Fade Float Function
How can I make a float fade using a function? Using it like FadeSomeFloat(myFloat, 0.0, 10.0, 30); and that would fade the variable "myFloat" from 0 to 10 in 30 seconds.
Most would be thinking "why not lerp?" but A: lerping is annoying as hell. and B: I need to run this in another function so that my fades to rain rather than just jumping to it, this means that I'm gonna need to fade like 6 different variables.
Do you want to call it only once or once every update?
Answer by Alec-Slayden · Jun 02, 2016 at 08:24 AM
As a technical answer to your question, you'll need to use either a coroutine or a manually called method that moves the value over time, but as far as I know there is no out-of-the-box single-call method, only interpolation utilities you will have to continuously call.
Essentially, you'll need to interpolate, however if you don't like Lerp, there is also SmoothDamp MoveTowards and for other use cases, InverseLerp
you could always write your own static utility class to implement a coroutine on the fly from a single call, but at that point you are starting to create the basis of a tween engine, a utility specifically designed to help animate or adjust values between two states.
Now, for the less technical answer to your question, there are several of these tweening engines freely available on the asset store, as well as some fine paid ones, that are simple and powerful, and can make your life much easier for fading colors, positions, and floats with a one-time method call, and it sounds like this might be a good solution for you. I do have my favorites, but rather than plug them I encourage you to take a look and see which solution will suit you best
Wow! This is probably the best answer I've gotten so far! (on the whole site)
This should work, I will give these a try and reply back in an hour or two and if it works then I will accept the answer.
Thank you very much! I tried to google a bunch of stuff but when I google anything it comes up with completely unrelated stuff for some reason, it just seems to be me that has the problem.
This worked flawlessly!
I'd searched for weeks/months trying to figure out how to fade a simple float, can't believe I was that stupid that I couldn't find this.
Then again, Google never shows anything relevant like I said before.
Couldn't figure out how to use SmoothDamp but $$anonymous$$oveTowards worked perfectly!
Now I just need to fade my day sky to my rainy sky and vise versa.
Now it isn't $$anonymous$$inecraft! $$anonymous$$y weather actually fades!
Answer by Zen_Hap · Jun 02, 2016 at 08:51 AM
Use a float gradient which is what you are saying. Just google Unity float gradient and it will come up. It works in a similar way to what you put but is slightly longer.
I hope I helped!
Your answer
Follow this Question
Related Questions
Ensure that function only gets called once in a frame 2 Answers
4.6 - Using new GUI with JavaScript functions 1 Answer
Problem accessing variable in javascript 1 Answer
How to fade in/out the view of a specific Camera? 2 Answers
Script is causing immense lag, and I don't know what's causing it. 2 Answers