Hi, Im struggling to compute distance mentioned in the title few days and come for help or some advices atleast.
My scroll view is horizontal ScrollRect with GridLayoutGroup as container.
Formula I found to compute passed distance:
distance = initSpeed * time - 0.5 * decelerationRate * time * time
Formula for decelerationRate:
decelerationRate = (finalSpeed - initSpeed) / time
Next Im trying to check are this formulas return acceptable values by placing list at center, appying some velocity to it, slowing it to zero in some time and comparing actual passed distance with formula’s values. You can check my code for that measurements below:
float secondsToScrol = 2;
float initSpeed = 1000;
GetComponent<ScrollRect>().normalizedPosition = new Vector2(0.5f, 0f);
yield return new WaitForFixedUpdate();
Debug.Log($"POSITION {container.transform.localPosition}");
float decelerationRat = initSpeed / secondsToScrol;
float distanceWhileSlowingDown4 = initSpeed * secondsToScrol - 0.5f * decelerationRat * secondsToScrol * secondsToScrol;
GetComponent<ScrollRect>().velocity = new Vector3(initSpeed, startPos.y, startPos.z);
float aniTime = 0;
while (aniTime < secondsToScrol)
{
GetComponent<ScrollRect>().velocity = new Vector3(Mathf.Lerp(initSpeed, 0, aniTime / secondsToScrol), startPos.y, startPos.z);
aniTime += Time.deltaTime;
yield return new WaitForSeconds(Time.deltaTime);
}
Debug.Log($"EXP {distanceWhileSlowingDown4} = {container.transform.localPosition}");
After executing this code Im getting next logs:
POSITION (0.0, 0.0, 0.0)
EXP 1000 = (1412.2, 0.0, 0.0)
Which say that actual passed distance is 1412, while computed by my formulas and expected is 1000.
Am I made some mistakes or to compute distance passed by slowing down movement in unity you need to use some specific formulas, different from real world physic ones?