I’ve tried asking this over on Answers, but I think because it’s such a newbie question people are glancing over it. I’ve been playing around with a couple of ways to do this but it doesn’t seem to be working. I’m trying to achieve the following - On button click, launching a script that changes a slider value from 0.85 to 1.15 over an amount of time. I’ve tried using :
in the hope that it would go from one value to the other over 5 seconds, but instead it just seems to change the value after 0.85 back and fourth (eg 0.85xxx). I’ve spent the last 2 days trying to get my head around why Lerp isn’t working and I’m pretty confident it’s something I’m using Lerp wrong. I am also wondering if using the Lerp command is even the best way to do what I’m after? I don’t want the value to change until the button is clicked, and once another button is clicked I want the value to go back to normal. How is the best way to achieve this? Any help is appreciated!
(Time.deltaTime / 5) is probably giving you a microscopically small number. deltaTime is the time between each frame Update() or something like that, as I recall. And, since you could be getting 100+ frames per second, that number is tiny.
If you use Lerp correctly, it should do what you are wanting, though. It’s designed to do what you think. It’s just a matter of getting it set up correctly.
The best article I’ve seen on using Lerps is the following:
Time.deltaTime represents the time between each frame. It will generally be a value that is very small, such as 0.01f, and it won’t fluctuate very much under normal circumstance. As far as the Lerp is concerned, Time.deltaTime is basically a constant that is very close to 0. This is sort of what the code is doing: Slider.value = Mathf.Lerp(0.85f, 1.15f, 0 / 5); every frame.
Instead, you need to add to an accumulated value, and lerp over that.
// delcare some value to store the accumulated time
float totalSliderTime =0f;
// inside of your update or animation method
totalSliderTime += Time.deltaTime;
Slider.value = Mathf.Lerp(0.85f, 1.15f, totalSliderTime / 5);
I suggest starting a coroutine to animate your slider value.
That coroutine mentioned by kru will work to get you there(to a value of 1, which would be the max value 1.15f), but it won’t get you back to a value of 0(which is where 0.85f is).
So, you are going to need to do some tweaking to get it to go back to 0.85f on the next button click.
Yeah see now I have more problems. The above code does indeed change the value of the slider, but it starts changing the second the program is run as opposed to starting on a button press. I can’t seem to find a way to do this because it has to go in Update or it only gets run once. I tried making a script to start another script on the button press, but that doesn’t seem to want to work either, again probably due to my lack of knowledge. The only way I can find out what to do is by using Google, and then I usually see several different ways to achieve it yet can’t seem to get any of them to actually work. The problem being, I actually have no idea what I’m really doing, I picked up Unity 4 days ago and have no experience with coding other than a small bit of ActionScript a few years ago, and even then I only learnt what I needed to at the time, and all has been forgotten, although it would be useless here. This really is the last bit of coding I need to get done, so I’m eager to get these functions working. I understand a whole lot more than I did 4 days ago, but in the grand scheme of things I still know nothing!
OK, remarkably I’ve got everything done, aside one thing… Resetting the value so each time the button gets clicked, it starts from 0.85F again instead of where it got stopped lerping. I’ve tried to reset the value of the slider back to 0.85F on click before the script containing the lerp command is executed, but after reading this post I understand it doesn’t matter what I set it to, the lerp needs to be reset to 0 because the lerp command will always pick up from where it left off in regards to the start and the end point, not a value I set. So my final question (I hope!) how do I reset the lerp? I’ve looked for answers and I’ve sat here trying for an hour or too, but it either throws me an error or it just doesn’t reset… Or I just don’t understand. This is the last thing I need to do before I can call it a day!
Yes I need to reset the value, but I can’t work out how. I have a script called Clicked which when clicked, enables the lerp script (lets call it LerpStart) to start doing it’s thing. That works fine. What I now need to work out is how to change the slider.Value in the LerpStart script from the Clicked script. I’ve Googled it and I keep finding answers, but they all seem to be different, and I can’t seem to implement them successfully!
That happily enables the LerpStart script, and the value starts to change. What I can work out is how (and to a degree, what) to tell the Clicked void to change the Lerp progress in the LerpStart script so I can set it back to 0.
The other thing I’m guessing now is by changing the slider.Value, I won’t actually be resetting the Lerp, I’ll just be resetting the starting point. Does that mean if the Lerp total time is 60 seconds and it gets reset 30 seconds though, the next Lerp will take the remaining 30 seconds or will it reset back to 60? I’ve used the following code from above:
IEnumerator UpdateSlider() //updates the slider value
{
float value = (float)currentXP / xpToNextLevel;
if (levelProgressSlider.value < value)
{
levelProgressSlider.value += 0.001f; ; //tick up
yield return new WaitForSeconds(0.001f); //each second
StartCoroutine(UpdateSlider()); //loop
}