Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active January 30, 2023 12:39
Show Gist options
  • Save unitycoder/d462b084ed38f3b32bbfc238c02df63d to your computer and use it in GitHub Desktop.
Save unitycoder/d462b084ed38f3b32bbfc238c02df63d to your computer and use it in GitHub Desktop.
Unity Physics



Update and FixedUpdate may be seen as two separate execution paths, not necessarily "after" or "before" each other.

Update

  • Executes on each visually presented frame. When you see 134 FPS in the stats it means 134 Update cycles per second.
  • Time.deltaTime is the time between one update cycle to another.
  • Updated for Unity 2020.2: If VSync is enabled then Update is called at the display's refresh rate and Time.deltaTime is the time between each frame presentation (typically 1 / refresh rate).
  • Frames may be skipped sometimes if the cpu/gpu load can't keep the display rate.

FixedUpdate

  • Executes at a fixed rate (50 Hz by default, defined in Project Settings > Time > Fixed Timestep).
  • Time.deltaTime is always the value specified in the Fixed Timestep setting.
  • FixedUpdate cycles won't be skipped ever: instead, the game time may be "slowed down" and/or Update calls will be skipped in order to perform every single FixedUpdate call.

There are situations where multiple FixedUpdate calls are performed between each Update call. Most frequently, Update is called several times between each FixedUpdate call.

Rule of thumb:

  • Update: visual stuff, camera, effects, things that may be adapted to varying delta time, and skipped for saving CPU/GPU.
  • FixedUpdate: physics, gameplay, AI, things that depend on precise timing and/or would affect gameplay if skipped.

Putting everything in FixedUpdate is incorrect. The game should be able to skip things to adapt to situations of intensive CPU/GPU usage. Otherwise, gameplay will just slow down in those cases.

LateUpdate executes in the same cycle as Update, but after all Update functions have been called. https://forum.unity.com/threads/update-vs-fixed-update-vs-late-update.307235/#post-5114657


Triggering AddForce in Update: Impulses are instant changes in the momentum, so it's ok to add them once when triggered. If you want to apply a standard force for some frames then you may use Input.GetKey (instead of Input.GetKeyDown) within FixedUpdate.

https://forum.unity.com/threads/triggering-addforce-in-update.719216/#post-4803791


you don't want to use Time.deltaTime with rigidbody.velocity. Due to the fact that it already moves your character at a speed that is framerate independent, using Time.deltaTime actually breaks things. It basically becomes framerate dependent again due to your velocity essentially being speed * Time.deltaTime * Time.deltaTime. So, just set rigidbody.velocity equal to Vector3(0,0,speed). It should work just fine.

https://forum.unity.com/threads/velocity-time-deltatime.91518/#post-592539


Rigidbody.velocity is an absolute velocity in m/s. You don't multiply it by deltaTime, just set the velocity value in the rigidbody. FixedUpdate should be used, but the resulting velocity would be the same if assigned from Update. https://forum.unity.com/threads/is-time-deltatime-necessary-for-rigidbodies.1067834/#post-6895091


Never use Time.deltaTime or Time.fixedDeltaTime in the calculation of a force. If delta time changes, for example by modifying the physics update rate, then the calculated forces (and the behavior) will be radically different.

Forces are time-independent magnitudes. You apply a given force in N (Newton) to the rigidbody. Once you've applied the force, the physics does the corresponding calculations along time (deltaTime) to get the updated velocities and positions.

In your code, if removing deltaTime gives too large forces, then simply reduce the values for rollSpeed/rollForce.

https://forum.unity.com/threads/sideways-movement-is-quicker-than-forward-and-back-when-adding-force-to-rigidbody.1360348/#post-8582203


the best practice is to use Time.deltaTime from both Update and FixedUpdate. The property will return the correct value depending on where it's read from. https://forum.unity.com/threads/a-comprehensive-guide-to-the-execution-order-of-unity-event-functions.1381647/#post-8707929


(Rigidbody) Interpolation is just a visual thing, it's not used in collision detection and only updates the Transform on the GameObject per-frame. The actual body just sits in its new position while the Transform interpolates from its last position to the new one. https://forum.unity.com/threads/moving-rigidbody-to-position-rigidbody-goes-throught-colliders.771659/#post-5140310

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment