- Home /
Third frame 'runs' several times
I'm trying to use Rigidbody2D.Addforce to apply a jumping force and gravity to my player object, but when I start the game I notice my player object teleports downward instead of gently falling down. The rest of my code works as intended, being completely smooth.
My jump code is all in FixedUpdate(), and at the end of it I have a print statement that returns the rigidbody's velocity vector and the current frame:
If I'm understanding this correctly, it means that the third frame is running several times? The third frame is the only time this happens. There are no loops in FixedUpdate(), and I cannot figure out how to stop this from happening.
What do you mean by "the current frame"? How are you counting?
He probably reads Time.frameCount. Though incrementing a counter in Update would yield the same result.
Answer by Bunny83 · Nov 21, 2017 at 04:30 AM
Have a look at this page and scroll down to the flow chart. Even FixedUpdate runs on the main thread it has no direct connection to the "visual" frames. FixedUpdate might execute several times in one frame or not at all for one or more frames. FixedUpdate is "fixed" (in the sense of adjusted).
Imagine we have set our FixedUpdate to run 50 times per second (that's the default). This equals a fixedDeltaTime of 0.02
seconds. Unity basically keeps track of two seperate "times". The first one is the normal game time (Time.time). It only advances in between visual frames. The difference between two frames is measured by Time.deltaTime. Of course depending on the speed of the hardware, if v-sync is enabled or not and the load of the game deltaTime can vary heavily.
If you divide "1f" by "Time.deltaTime" you get the current framerate. Likewise if you would divide 1f by "Time.fixedDeltaTime" you get the fixed frame rate. Unlike Time.deltaTime, Time.fixedDeltaTime is not calculated based on performance but is actually a constant value that is set by you / Unity. It dictates / controlls how often FixedUpdate should run per second.
What Unity does each frame is it compares Time.time with Time.fixedTime in a while loop. as long as the game time is larger than fixedTime it runs one "physics step". This involves all the physics calculatesion as well as calling FixedUpdate once. At the end of each physics stel the fixedTime is increased by fixedDeltaTime.
Imagine your game only runs at 25 fps. That means deltaTime is "0.04" seconds. So after the first frame Time.time would be at 0.04. However the fixedTime is still at 0. So Unity calls FixedUpdate and fixedTime will by 0.02. This is still smaller than 0.04 so it runs another step and fixedTime goes up to 0.04. Now we catched up with the game time and the frame can continue. In this case FixedUpdate runs approximately two times every frame since the visual framerate is two times slower than our set fixed frame rate of 50.
Imagein your game runs at 100 fps. That means deltaTime is about "0.01". So it takes two visual frames until the game time actually passes the fixedTime. In this case FixedUpdate is only called every second frame.
If your framerate drops to a very low value (deltaTime is rather high) FixedUpdate will run more often. If you do something really heavy in one frame that freezes your game of 0.2 seconds Unity will run FixedUpdate 10 times in a row in order to catch up to the game time.
That's why you should only do things in FixedUpdate that are related to physics. Also you can't read any one time input inside FixedUpdate (GetXXXXDown / GetXXXXUp). Those would either be recognised several times (in case the framerate is low) or it wouldn't be detected at all because FixedUpdate did not run the frame where the event occurred. FixedUpdate should only be used when you apply a continous force over time. One time impulses should be applied in Update since they need to be in sync with the input.
Your answer
Follow this Question
Related Questions
Rigidbody2D.velocity.x not working but Y does 0 Answers
How can I tweak acceleration and deceleration of a Rigidbody2D with .AddForce()? 0 Answers
Dashing with rigidbody2D not working right 2 Answers
move 2d character affected by physics with velocity and/or add force 2 Answers
,Rotate a gameobject around another while being attracted by its gravity 1 Answer