• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 10, 2022 at 03:27 PM by Bunny83 for the following reason:

The question is answered, right answer was accepted. Closed because the answer should explain everything there is to say. If you think you have to add something constructive, write me (or another moderator) a PM on the forum.

avatar image
37
Question by Micha Lewtak 2 · Feb 02, 2010 at 05:50 PM · movementphysics

What's the difference between Update and FixedUpdate? When are they called?

I'm making my own simple physics as I need more control and knowledge over what's going on in my very physic-driven game, and I just figured out that I can't update my velocity (final translation value) in Update(), because when the frame rate isn't fixed, I won't get the same final position after 5 seconds of game each time. I'd like to understand how precisely FixedUpdate() works, because the following explanation, taken from the Event Execution Order article on Unifycommunity, doesn't explain much to me:

Physics is run in a loop, iterating over the following until physics has caught up to the current frame:

* FixedUpdate
* Physics simulation

(...)

Say we have two timelines, and on the first one, we have fixed updates as blue rectangles, and on the second one we have normal updates (always as short as possible depending on processor and rendering) as red rectangles. How are they laid out? Are the normal updates delayed if fixed updates are still being processed? Or do the normal updates always just consider the last fixed update even if it was long time ago? I made a raycasted collision system and t$$anonymous$$ngs never fly through anyt$$anonymous$$ng, no matter what the speed is, and I'd like to keep it that way now.

Comment
Comments Locked
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

  • Sort: 
avatar image
117
Best Answer

Answer by duck · Feb 02, 2010 at 09:38 PM

As far as I understand it, the two different update functions work somet$$anonymous$$ng like t$$anonymous$$s. I'm writing it in the form of pseudocode first, and adding diagrams below, w$$anonymous$$ch may or may not make it clearer to understand!

In the (completely fictitious) code below, first the appropriate number of physics steps are executed in order to "catch up" with the current time (and each step, FixedUpdate() is called on each object w$$anonymous$$ch implements it). Next, the grap$$anonymous$$cs for the frame are rendered, followed by Update() on each object w$$anonymous$$ch implements it.

 var physicsTimeSimulated = 0;
 var lastUpdateTime = 0;    
 
 w$$anonymous$$le (Unity is Running)
 {
     w$$anonymous$$le (physicsTimeSimulated < Time.time)
     {
         Engine.ExecutePhysicsStep();
         Engine.FixedUpdate(); // <-- sent to all objects
         physicsTimeSimulated += physicsTimeStep;
     }
 
     deltaTime = Time.time - lastUpdateTime;
     Engine.RenderFrame();
     Engine.Update(); // <-- sent to all objects
     lastUpdateTime = CurrentTime;
 
     // and repeat...
 }

It's worth noting that if the game is running at a slow frame rate, there will be numerous physics updates between each visible frame render. Conversely, if the game is running at a very $$anonymous$$gh frame rate, there may be no physics steps at all between some of the frame renders, because the time elapsed since the last rendered frame has not yet exceeded the time period of a single physics step.

If the physics timescale is left at its default value (0.02) t$$anonymous$$s gives us 50 physics updates per second - each physics step simulates the motion that occurs over the period of two-hundredths of a second.

The diagram below shows a period of one-tenth of a second. The dots w$$anonymous$$ch break up the line indicate 100th's of a second.

(I'm using an "F" to show where the FixedUpdate calls go)

0                                                0.1 seconds
|                                                 |
.____.____.____.____.____.____.____.____.____.____.___ 
F         F         F         F         F         F

Now, if our game were running nice and fast at 100fps, we'd have two frame renders for every physics step - and therefore two calls to our Update() functions, for every call to our FixedUpdate() functions. (Key: "F" for FixedUpdate and "U" for Update)

0                                                0.1
|                                                 |
.____.____.____.____.____.____.____.____.____.____.___ 
F         F         F         F         F         F
 U    U    U    U    U    U    U    U    U    U    U

If our game is running slower, say - at 30 frames per second (and therefore 30 calls to all Update() functions per second), it would mean that we actually sometimes have more than one physics step between each frame render. In the case of 30 fps, the result would be that sometimes two physics steps are executed between frames, and sometimes one, w$$anonymous$$ch would look somet$$anonymous$$ng like t$$anonymous$$s, for the first 10th of a second:

0                                                0.1
|                                                 |
.____.____.____.____.____.____.____.____.____.____.___ 
F         F         F         F         F         F
 U                U               U                U

So, in most normal circumstances, you'll always get the desired number of physics steps per frame, and interleaved with these will be the visual frame updates, at as fast a rate as possible.

It's for t$$anonymous$$s reason that FixedUpdate should be used when applying forces, torques, or other physics-related functions - because you know it will be executed exactly in sync with the physics engine itself.

Whereas Update() can vary out of step with the physics engine, either faster or slower, depending on how much of a load the grap$$anonymous$$cs are putting on the rendering engine at any given time, w$$anonymous$$ch - if used for physics - would give correspondingly variant physical effects!

The exception to t$$anonymous$$s would be that if your scene was putting such a load on the physics engine that it approaches the point where it becomes impossible to execute the required number of physics time steps to keep up to speed with 'real time'. T$$anonymous$$s means that your game has become impossible to simulate in real time - and in t$$anonymous$$s case you need to seriously t$$anonymous$$nk about redesigning your game! T$$anonymous$$s can happen if you have large numbers of complex objects (eg, rigidbody mesh colliders) all clumped together so you have lots of many-to-many collisions occuring each step.

Hope t$$anonymous$$s sheds some light on the comings and goings of FixedUpdate and Update!

Comment
Comments Locked · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Micha Lewtak 2 · Feb 03, 2010 at 07:14 PM 0
Share

Thanks, that really helped me out. Now I have another question - if we set fixed step time to some high value like 0.05, we'll have our physics running at 20 fps. So in the game, movements will seem to lag. Because assuming the game's running at 60 fps, only once per 3 frames will bodies' positions update. Kinda like in Bioshock for some people. Am I right here? Because if so, I don't wanna use Unity's physics for my game, which is All about physical movement...

avatar image duck ♦♦ · Feb 03, 2010 at 08:30 PM 0
Share

Well, yes - if you set the physics engine to operate at a low rate of update, it will do just that. The effects can be smoothed over by turning on rigidbody.interpolation - but why would you want to set it to a value like that in the first place?

avatar image duck ♦♦ · Feb 03, 2010 at 08:31 PM 0
Share

Usually speeds like that are only used on iPhone, to ease the load on the cpu. For desktop builds, its commonly adjusted in the other direction for greater accuracy and to avoid the 'bullet through paper' problem - particularly for high-speed games like racing games.

avatar image amanpu · Aug 17, 2015 at 10:54 AM 3
Share

Nice explanation :), Please rewrite the entire Unity Documentation Please

avatar image aksh2143 · Sep 23, 2015 at 12:56 PM 0
Share

@duck Is there any proper way to get knowledged like you? :) _/_

avatar image daveScof aksh2143 · Mar 19, 2016 at 11:59 PM -1
Share

Very Awesome Reply!! @aksh2143 I would assume looking at the short video on http://unity3d.com/learn/tutorials/modules/beginner/scripting/update-and-fixedupdate would be a start :)

Show more comments
avatar image
17

Answer by Eric5h5 · Feb 02, 2010 at 08:40 PM

Update runs once per frame. FixedUpdate can run once, zero, or several times per frame, depending on how many physics frames per second are set in the time settings, and how fast/slow the framerate is.

Comment
Comments Locked · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Karsnen_2 · Sep 18, 2012 at 12:12 AM 0
Share

I just have a small doubt.. Say, I calculate values to add force on rigidbodies...

is it advisable

a) Get the values in Update() and then apply on FixedUpdate()

or

b) Get Values in FIxedupdate and apply in FIxedUpdate()

avatar image Paulo-Henrique025 · Sep 18, 2012 at 12:31 AM 1
Share

If you get the data from Update it maybe inaccurate.

avatar image Bunny83 · Sep 18, 2012 at 01:23 AM 3
Share

Ohh, another thread about FixedUpdate, so i link my FixedUpdate simulator just as reference ;)

avatar image Paulo-Henrique025 · Sep 18, 2012 at 01:40 AM 0
Share

Its from February haha! Thanks for that link!

avatar image Bunny83 · Sep 18, 2012 at 01:42 AM 0
Share

Yes, February 2010 ;)

Show more comments
avatar image
3

Answer by jpierre88 · Jan 21, 2014 at 10:40 PM

T$$anonymous$$s short video clarified it for me: http://unity3d.com/learn/tutorials/modules/beginner/scripting/update-and-fixedupdate

Comment
Comments Locked · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syamilsynz · Aug 24, 2014 at 04:23 PM 0
Share

thats the answer!

avatar image centerforabcs · Aug 26, 2020 at 07:55 AM 0
Share

Link has changed: https://learn.unity.com/tutorial/update-and-fixedupdate

avatar image
0

Answer by Ashkan_gc · Feb 02, 2010 at 07:33 PM

fixed Update garanteed to run and if you make them heavy then your framerate will drop. you can easily test it. reduce the fixed update in edit/project settings/time and see how much frame rate drops. change t$$anonymous$$s fixed time value to a number as large as possible. i t$$anonymous$$nk unity first run all FixUpdates and then run an update and render a frame.

Comment
Comments Locked · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by sillyjake · Aug 28, 2014 at 02:50 AM

Basically, without going into much detail, Update is drawn every frame, W$$anonymous$$le FixedUpdate is drawn before physics calculations.

Comment
Comments Locked · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zarkow · Aug 06, 2015 at 04:31 PM 0
Share

Do you mean 'called'?

In FixedUpdate we deal with anything related to physics. And tied in with each FixedUpdate call (presumably right after exiting FixedUpdate for all objects), Unity's own physics-machine run one tick, lock-stepped.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Finding the axis of a curving pipe 0 Answers

Force to Velocity scaling? 2 Answers

Quake like movement 1 Answer

Hit problem unity 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges