Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

FixedUpdate()

Discussion in 'Scripting' started by Wasiim, Jun 10, 2015.

  1. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    Why do you have to use FixedUpdate for physics. What is wrong with using Time.deltaTime in update() i mean what is wrong with compensating the force upon an object using addForce() with Time.deltaTime? sure the frames in update() take different amounts of time to complete but when you are using Time.deltaTime is it possible to compensate the amount of force on a object from one frame to another?
     
    Last edited: Jun 10, 2015
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
  3. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    Well just a guess since you are utlizing Time.deltaTime and i am assuming one frame took long. It compensates that frame
    so that even though it took 2 secs it adds force equals to multiple frames it might have completed in that time period.
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Assume you are adding forces when, say, keypresses happen. And you tap "A" and add force Vector3.Left*Time.deltaTime. Then you tap "D" and Vector3.Right*Time.deltaTime just as the game hangs for a couple of seconds.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I believe doing it in Update would make the physics less predictable, and therefore less reliable when it comes to collisions.
     
  6. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    You say that but what if the game hangs for 2 secs in FixedUpdate() during the 0.02 time interval each frame takes to complete?
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    A frame doesn't take 0.02secs, it can take whatever. Also not sure what you mean by "hangs on fixed update". Fixed update can be ran multiple times per frame if needed (or just once, or none, depending on fixed update rate and actual framerate).

    Did you read the link I posted? It explains it quite nicely.
     
  8. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    Yes i did read it but i am a noob i do not understand anything that is going unless it applies to me. I cannot relate give me a simple scenario and i will understand.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Building a simulation for physics with a variable time step is a night mare. Believe me, I've done it. Getting consistent results is pretty difficult. Things like collision checking, applying forces, drag and friction all have much simpler calculations if they are done with a discrete time step. The simulation happens for 0.02 seconds, then all of the various forces are calculated, then simulation, then calculations. Rinse and repeat forever.

    The folks at NVIDIA came to the same conclusion when they built their PhysX. Simulated physics is just far, far simpler with a discrete simulation.

    So Unity comes along and says "What is the best physics engine for games on the market". The answer is PhysX. Since PhysX is built around a fixed time step, it makes sense to interact with PhysX using a fixed time step. Thus FixedUpdate is born and created.

    You don't have to use FixedUpdate for physics, but expect to see some inconsistent results if you don't. Occasionally jumping twice as high as normal is a common one.
    • Player holds down jump key
    • Update runs, player is grounded. Force up is added
    • Update runs, player is grounded. Force up is added
    • FixedUpdate runs. Force is added twice. Player moves, and is no longer grounded
    • ...
    • Players final jump height is twice the expected height, because of the double force added.
     
    ramand likes this.
  10. jleslie

    jleslie

    Joined:
    Mar 24, 2013
    Posts:
    12
    Imagine a car, with the engine as your AddForce().

    Now imagine driving your car down a freeway with your foot held on the throttle at a certain postition, your ride will be smooth and you will always know how fast and how far you are traveling at any given moment. This is FixedUpdate...

    Now imagine traveling down the highway and slamming your foot on and off the throttle, sure you can time it to reach your destination in the same amount of time but you're going to be jerked back and forth and in for a bumpy, uneven, ride and never quite sure how fast you may go at any second of your ride. This is Update().