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?