I am trying to create a smooth function to interpolate a number to zero. Problem is, it isn't working. In this case, if I increase a number by 0.01 a specified number of times, I want to decrease that number by 0.02 until it is zero. Here is my function.
float ReduceSpeed(float x)
{
if (x != 0f)
{
if (x % 0.02f != 0 && x > 0)
{
x = x - 0.01f;
} else if (x % 0.02f != 0 && x < 0)
{
x = x + 0.01f;
}
else
{
if (x > 0f)
{
x = x - 0.02f;
}
else
{
x = x + 0.02f;
}
}
}
return x;
}
I want to use this to create a kind of sliding motion in unity.