Lerp doesn’t do what you think.
What it does is return a value that’s between the first two parameters, using the third parameter to specify how far between.
Axis = Mathf.Lerp(Axis, 1f, 0.5f);
The above usage will set Axis to be half way between what Axis currently is and 1, because you’re passing in 0.5 as the third parameter.
Assuming Axis starts off at zero (and this is currently not reliable because you have not initialised it in your script) then we can look at what will happen to it if we run the above code repeatedly (ie. over a few frames.)
- 1st frame: Axis changes from 0.0 to 0.5.
- 2nd frane: Axis changes from 0.5 to 0.75.
- 3rd frame: Axis changes from 0.75 to 0.875.
- 4th frame: Axis changes from 0.875 to 0.9375.
And so on.
This demonstrates three things:
1: Using 0.5 does not directly tell anything to happen over a certain amount of time. It controls the speed of change by saying how big a step Axis will change by each time you call lerp. Using 0.5 means step by half the difference between the first two parameters.
2: If you are running at a high frame rate then it will still take the same number of frames to change Axis, but it will happen a lot quicker. This is why you see the use of Time.deltaTime coming into some of the examples.
Axis = Mathf.Lerp(Axis, 1f, 0.5f * Time.deltaTime);
This will make the behaviour frame rate independent by adjusting the third parameter to reflect how long it’s been since the last update. Once you have this setup, you can try using different values in place of the 0.5 to tune the speed of the lerp and get the behaviour that you want.
3: When using lerp like this your Axis value will increase by a smaller and smaller amount as it approaches the second parameter. It’s important to appreciate this because what it means is that it’s not sensible to rely on a lerped value actually reaching the target. It just gets closer and closer, but more and more gradually with each update. This means that you should always check for reaching the top end by using a slightly more tolerant check, as follows…
Axis = Mathf.Lerp(Axis, 1f, 0.5f * Time.deltaTime);
if ((1f - Axis) < 0.01f)
{
// Got there - well, near enough!
}
The trouble you’re having when you’re not pressing is related to point 3. It is actually lerping towards zero, but the value is getting so small that ToString() has started to represent it in scientific notation. The E-11 in the ouput can be interpreted as “really, really, really tiny”. 1.455192E-11 is just a compact way of writing 0.00000000001455192. Your Axis has shot towards zero very quickly, but it won’t actually ever get there because each time lerp is called, it’s taking a smaller step towards zero than the previous time. In fact, the step is half the size each time because of your use of 0.5. Try running your above code with a value of 0.005 instead of 0.5 and you’ll see in your output that the value of Axis still ends up in scientific notation, but it will take a lot longer because it’s being adjusted by a much smaller step each frame.
Well… I hope this helps. Good luck.