Hello all. I have 2 UI buttons that move my player UP and DOWN along the Y axis. I’m using CrossPlatformInputManager.GetAxis (“Verticle”); for the touch input and movement. My question is simply how can I alter or write a script that will move my player smoothly? Perhaps inputing MathF.Larp?
Keyboard button input moves my player very smoothly and I cant seem to do the same with the touch buttons.
I adapted the following code from an answer I posted in another thread where someone wanted to get smooth movement when triggering a camera pan when the mouse was at either edge of the screen, but it should be re-useable here. Pass the function your calculated directionY, and where you set rb.velocity, multiply moveSpeed by the result it returns.
public float timeToFullSpeed;
float speedTimer;
float GetSpeedMultiplier(float directionY)
{
if (directionY > Mathf.Epsilon)
{
speedTimer = Mathf.Min(timeToFullSpeed, speedTimer + Time.deltaTime);
}
else if (directionY < -Mathf.Epsilon)
{
speedTimer = Mathf.Max(-timeToFullSpeed, speedTimer - Time.deltaTime);
}
else
{
if (speedTimer > 0)
{
speedTimer = Mathf.Max(0f, speedTimer - Time.deltaTime)
}
else
{
speedTimer = Mathf.Min(0f, speedTimer + Time.deltaTime)
}
}
return speedTimer / timeToFullSpeed;
}