- Home /
Rigidbody character and Update() function
So in official Unity's 2D Character Controllers tutorial ( http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers ) they made their character out of a physics2D rigidbody. That's kind of cool, but if they want to move this character, they must do it in an FixedUpdate() function (since it's a rigidbody).
But what about the DeltaTime stuff? The movement can now get ahead or behind the frame rate, right? Can't this be fixed? Does it mean that you can either have a smooth game, or a physics-based game, but not both?
I don't really understand. FixedUpdate still makes your game smooth, it just runs all code within it less times, but it doesn't impact your frame rendering.
Yes but, since the rendering is done in the Update() function, it can de-smooth the movement of objects by rendering them in different positions for different amounts of time. Example: Let's say that my FixedUpdate runs every second. We have an object that gets translated by 5 units with each FixedUpdate call. Now, the Update() function, which renders the screen, gets called at random intervals. Let's say that in this case, it gets called at 0.1, 0.9, 1.4 and 2.0 in-game seconds. This means that during the first frame (0.1), our object is at position 0. During the second frame (0.9), our object is still at position 0, since we only move it in the first FixedUpdate call (which will be called in 1.0). During the third frame (1.4), our object has finally moved and is now in position 5. During the fourth frame (2.0), our object has moved once again and is now in position 10.
So, we have an object that should move in constant speed, but it doesn't appear like that because it sometimes takes him more and sometimes less frames to move (it took him two frames to move from 0 to 5, but only one frame to move from 5 to 10).
Answer by whydoidoit · Feb 28, 2014 at 12:18 PM
In FixedUpdate you use Time.fixedDeltaTime instead of Time.deltaTime.
I don't understand, how does this fix anything? I might have explained myself incorrectly - see my reply to Danzou above.
That's not how physics works. The game has a flexible frame rate, every time a frame starts it calls FixedUpdate the number of times necessary to catch the physics time up to the frame time. If your frame rate is super high, you can decrease the time between physics steps if you like so that it runs frequently - if you run at 100 fps you should decrease the physics time to make sure that you keep running the physics step often enough etc. (otherwise it would only run every other frame with the standard setting)
Inside FixedUpdate you use fixedDeltaTime to smooth out your movements to make them frame rate independent.