Course contentsShow
← Unity Game Development
Lesson 121 of 8282. Scripting and LifecycleFree lesson

Applying Consistent Forces in FixedUpdate

Utilize the physics-based FixedUpdate loop to apply forces that remain stable and predictable across different hardware configurations.

Applying Consistent Forces in FixedUpdate

In this lesson, you will learn how to use the FixedUpdate loop to apply physical forces that behave reliably regardless of your game’s frame rate.

Why Update Isn't Always Enough

Up until now, you have used Update to handle game logic. Update runs once per frame, which means if your game lags or if a player has a high-refresh-rate monitor, the frame rate fluctuates. If you apply a physical force (like an explosion or gravity) inside Update, the object will react differently depending on how many frames per second the computer manages. This makes physics unpredictable and "jittery."

The FixedUpdate Advantage

Unity provides a dedicated loop called FixedUpdate specifically for physics calculations. Unlike Update, FixedUpdate runs on a strictly consistent timer—by default, 50 times per second.

When you apply forces using the physics engine inside FixedUpdate, Unity calculates the motion in uniform time steps. This ensures that a ball pushed with the same force will travel the exact same distance whether your computer is running at 30 FPS or 300 FPS.

Implementing a Force

To apply a force consistently, you first need a reference to the Rigidbody component. Instead of putting your logic in Update, you define it inside FixedUpdate.

using UnityEngine;

public class PhysicsPusher : MonoBehaviour
{
    private Rigidbody rb;
    public float forceAmount = 10f;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        // Applying a constant upward force every physics step
        rb.AddForce(Vector3.up * forceAmount, ForceMode.Force);
    }
}

By placing rb.AddForce here, you guarantee that the engine processes that force at the exact same intervals, keeping your game's movement stable and predictable. If you performed this in Update, the object would fly much higher on fast computers because it would receive the "add force" command more frequently. Using FixedUpdate keeps your physics engine in perfect sync with the game's simulation time.

Key Takeaway: Always use FixedUpdate for physics interactions to ensure consistent movement and stable behavior across all hardware configurations.