To calculate its next position, I am using the below formula:
nextPosition = currentPosition + direction * movementSpeed * deltaTime.
Upon having all 3 gameobjects move off at the same time, the 1st and 3rd objects appear to move at the same rate while the 2nd lags severely behind. Why is that?
The physics simulation runs on a fixed update time, hence why FixedUpdate should be used for physics tasks like moving a Rigidbody.
On the other hand, Update is tied to frame-rate; your frame-rate is how many times Update executes per second.
Since the physics simulation isn’t tied to frame-rate, it’s possible for nothing to happen on a frame where you try to execute some physics task, because the next fixed time step hasn’t been updated yet on that frame.
Thank you for the reply. In that case, could you confirm that the logic in the below code is flawed for gameobjects with rigidbodies? (Assuming FinalizePosition is called in Update() and the supplied parameter “nextPosition” is calculated in Update() with Time.deltaTime)
If it is true that the above code is flawed, would it be appropriate for one to add checks in the calculation of “nextPosition” to have different calculations for gameobjects with rigidbodies? Below is an example to illustrate what I am thinking of:
The typical way to share data between Update and FixedUpdate is to just use a global variable.
I may be wrong here, but in your CalculateNextPosition method, I believe transform.position + direction * movementSpeed * Time.deltaTime; should work fine regardless if you’re using a Transform or Rigidbody for movement; you just need to make sure the next position is calculated from Update, and used in FixedUpdate when applicable:
Thanks for getting back, that indeed sounds like a good way to share data between Update() and FixedUpdate(). I have already incorporated it into my project and its working well so far.
I also went ahead to test out your script. However, it seems that gameobjects with a rigidbody still moves slower compared to the ones without. I think this is because when a position is calculated in Update, as you said, some frames are rendered without the physics simulation have run its update cycle, thus making it possible for CalculateNextPosition to calculate the same position over a few frames, hence leading to the object moving slower than intended.