Dragonfly Cloud is now available on the AWS Marketplace - Learn More

Question: When should you use FixedUpdate in Unity?

Answer

Unity offers different methods for updating game elements: Update(), LateUpdate(), and FixedUpdate(). The choice between them depends on what kind of action you want to perform.

FixedUpdate() is called at a regular interval, determined by the physics settings in Unity (accessible via Edit -> Project Settings -> Time, under the Fixed Timestep option). This makes it ideal for handling anything related to physics calculations because it keeps them consistent regardless of the frame rate.

Here are some scenarios where you should use FixedUpdate():

  1. Rigidbody Physics: When applying forces, torques, or other physics-related functions to a Rigidbody, do it within FixedUpdate().

  2. Consistent Timing: Operations that need to happen at consistent times, such as a clock tick or regular checks.

  3. Physics-Based Movement: If you are manually calculating movement for an object and want it to be smooth and consistent with the physics engine.

Here is an example of using FixedUpdate() to apply a force to a Rigidbody:

using UnityEngine; public class Mover : MonoBehaviour { public float force = 10f; void FixedUpdate() { GetComponent<Rigidbody>().AddForce(transform.forward * force); } }

Remember not to use FixedUpdate() for general gameplay logic, input reading, or animations, as those might not require the consistency of the physics system and could lead to issues if they're out of sync with the frame rate (use Update() for those cases instead).

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost