Game behaves differently on different frame rates despite using Time.deltaTime

Hi everyone!

In my (android) game I use Vector3.Lerp inside a coroutine loop to move an object from one position to another. However, even though I use Time.deltaTime, the object moves faster when I run the game at 120fps instead of 60fps.

Here is the main code that handles the movement of objects:

public class ObjectController : MonoBehaviour
{
    public GameObject pointA;
    public GameObject pointB;
    Vector3 startingPosition;
    Vector3 targetPosition;
    //Time it takes to reach the end position
    public float timeToLerp;

    public UIManager uiManager;

    private void Start()
    {
        AssignPositions();
    }

    private void AssignPositions()
    {
        startingPosition = pointA.transform.position;
        targetPosition = pointB.transform.position;
    }

    public void MoveObject()
    {
        transform.position = startingPosition;
        StartCoroutine(MoveObjectRoutine());
    }

    IEnumerator MoveObjectRoutine()
    {
        bool hasReachedTargetPosition = false;
        float elapsedTime = 0f;

        while (!hasReachedTargetPosition)
        {
            if (Vector3.Distance(transform.position, targetPosition) < 0.001)
            {
                transform.position = targetPosition;
                hasReachedTargetPosition = true;
                if (Application.targetFrameRate == 60) uiManager.timeTook60Text.text = elapsedTime.ToString();
                else if (Application.targetFrameRate == 120) uiManager.timeTook120Text.text = elapsedTime.ToString();
                else if (Application.targetFrameRate == 30) uiManager.timeTook30Text.text = elapsedTime.ToString();

                Debug.Log(elapsedTime);

                break;
            }

            elapsedTime += 2 * Time.deltaTime;
            float t = elapsedTime / timeToLerp;

            transform.position = Vector3.Lerp(transform.position, targetPosition, t);

            yield return null;
        }
    }
}

Here are the results when I run the game on my mobile device, where Time Took for each frame rate displays the elapsedTime when the object has finished Lerping:

As you can see the object reaches its target position faster the higher the frame rate is. Why is that? Am I doing something wrong?

Essentially, what I am trying to achieve is for the object to take the same amount of time to Lerp between two positions, no matter what the frame rate is.

Am I missing something? Isn’t Time.deltaTime supposed to make your code frame rate independent? Why does it behave differently on different frame rates?

Thank you for your time!

You’re doing your Lerp incorrectly, which is introducing some framerate dependency into he mix. you have this:
transform.position = Vector3.Lerp(transform.position, targetPosition, t);But the correct way to do it is this:transform.position = Vector3.Lerp(startingPosition, targetPosition, t);Where startingPosition is the position your object started at, which you should save at the beginning of the coroutine, before the loop starts. It should not change over time.

1 Like

This is it! From the bottom of my heart, thank you!