Would increasing speed by a fixed number on Update make it framerate dependent?

Hi everyone, this question came to me as I was watching the “Pickups” lecture on this course and noticed the speed of the coin was a bit different from the one I had on my game even though the code was the same, so it made me think that increasing the speed by a fixed number would make it framerate dependent.
I know Update runs once per frame so if we increase the speed by, say, 1 per frame, on a PC running 30fps we would end up with 30 speed, and a PC with 60fps will end up with 60 speed. Should we multiply by Time.deltaTime acceleration too?
I don’t know, maybe I’m wrong because I saw in another post that it is recommended that we change the FixedUpdate method Time.deltaTime for Time.fixedDeltaTime, so maybe doing that would offset the framerate dependency I was talking about? My logic tells me that it doesn’t since acceleration is still increasing faster on a faster PC, but I am not sure.

Also, I tried swappingTime.deltaTime for Time.fixedDeltaTime on fixedUpdate and I could not notice any difference.
So I came up with this small modification:

private void Update()
    {
        Vector3 playerPos = PlayerController.Instance.transform.position;
        if(Vector3.Distance(transform.position, playerPos)<= pickupDistance)
        {
            moveDir = (playerPos - transform.position).normalized;
            moveSpeed += accelartionRate * Time.deltaTime;
        }else
        {
            moveDir = Vector3.zero;
            moveSpeed = 0f;
        }
    }

    private void FixedUpdate()
    {
        rb.velocity = moveDir * moveSpeed * Time.fixedDeltaTime;
    }

This works and I think it’s framerate-independent BUT I have to put really high numbers on the acceleration rate, so I’m not sure if this is OK.
What do you guys think about this? :slight_smile:

I’m pretty sure you’re correct! Regardless of anything else, multiplying by Time.deltaTime in Update makes moveSpeed framerate independent; which it otherwise wouldn’t be. Using a (much) higher number for accelartionRate shouldn’t cause any problems as long as it’s moving at the speed you want.

Update and FixedUpdate don’t run at the same rate, so it’s always advised to use Time.deltaTime in Update and Time.fixedDeltaTime in FixedUpdate.

1 Like

This solution is perfect for making acceleration frame rate independent.

The reason for the wild differences in values comes when you consider the function of each of the two variables…
moveSpeed == the current rate of travel. Compare this to your car going 55 miles per hour.
accelarationRate == the current rate of increase to the speed. Compare this to when you press on the accelerator in your car.

You can maintain a rate of speed with a relatively low number. Each frame, you will move just what is needed to maintain a constant speed of moveSpeed. With Acceleration, however, you require a higher rate to move from zero to your desired speed.

As I drive down the freeway at 55, I usually have my gas pedal depressed about 1/4 of the way. To get to 55, however (at least if I want to do so quickly), I have to press much harder on the gas pedal until I reach that speed and my momentum will allow me to back off the accelerator.

I hope my makeshift physics analogy made sense.

2 Likes

Thank you, yes the analogy is very clear!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms