docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use physics with predicted GhostObjects

    Set up predicted GameObject physics, and write collision and trigger handling that stays correct when prediction replays a tick.

    GhostObjects work with the built-in 3D physics system. When you predict a Rigidbody, GhostObjects step physics inside the prediction loop instead of during FixedUpdate, so that physics runs as similarly as possible on clients and the server.

    This page explains how to set up predicted physics, and how to handle physics events so they stay correct when prediction replays ticks. For background on the prediction loop, refer to Rollback and replay.

    Set up predicted physics

    By default, Netcode replicates Rigidbody data, such as velocity and sleep state, and rolls it back whenever your ghost can be predicted. Replicating this data uses bandwidth, but it gives you tick-accurate physics. To disable velocity replication, disable Rigidbody Replication on the GhostObject component in the Inspector window. This removes that body from the predicted physics simulation.

    By default, Netcode doesn't replicate Rigidbody data for interpolated ghosts.

    Disabling Rigidbody Replication doesn't exclude a dynamic Rigidbody from prediction replay. Unity Netcode steps the whole default physics scene on each replayed tick, so it resimulates a dynamic body that has no replication without rolling it back. Move client-only physics, such as debris, to a separate physics scene created with LocalPhysicsMode.Physics3D using a proxy physics scene.

    To get as much tick accuracy as possible between clients and the server, configure the physics settings:

    1. Go to Edit > Project Settings > Physics.
    2. Set Solver Type to Projected Gauss Seidel. Temporal Gauss Seidel is less deterministic for prediction.
    3. Enable Enable Enhanced Determinism. This setting doesn't give full bit-by-bit rollback determinism, so the server still validates the result.

    These settings make physics results on clients and the server as similar as possible.

    Use PredictionPhysicsUpdate for logic that must stay in sync with the predicted physics simulation, in the same way that FixedUpdate stays in sync with the offline physics simulation. PredictionPhysicsUpdate runs before the engine's simulation step.

    For a complete example, refer to the physics sample in the Assets/GameObjectSample/1_Features/0_Physics folder of the Netcode samples project. It moves a player character by setting its Rigidbody velocity in PredictionPhysicsUpdate.

    Handle physics events

    Prediction replays ticks, so Unity runs your physics messages again for each replayed tick. An unguarded collision handler therefore applies its effect several times on a predicting client, and once on the server.

    Understand when physics messages run

    Unity Netcode sets Physics.simulationMode to Script and steps physics itself, once per full tick, inside the prediction loop. Each step publishes its contact results, which is when Unity calls your physics messages. OnCollisionEnter and similar messages all run on the tick that produces the contact.

    On a client, frames 100, 101, 102, and 103 can all run the same tick 10. Depending on the logic, you might need to guard your handler with Netcode.Time.IsFirstTimeFullyPredictingTick.

    Guard anything that spawns an object, plays audio or a visual effect, sends a remote procedure call (RPC), or accumulates a value that isn't a GhostField. A GhostField doesn't need the guard, because Netcode rolls it back before it replays a tick. Values that you recompute from scratch each tick don't need a guard either, because replaying them produces the same result. This is the same rule that applies to PredictionUpdate. For more information, refer to Trigger visuals only once from prediction and Managing latency with prediction.

    The server and partial ticks behave differently from a predicting client:

    • The server runs your handler once per tick. It simulates each tick a single time, so an effect that duplicates on a client still looks correct on the server.
    • Partial ticks don't step physics. Unity Netcode skips them, so no physics message runs on a partial tick. Partial ticks are off by default in new projects, with Partial Tick Mode Selection on the NetcodeConfig asset set to Full Ticks Only.

    Treat a predicted contact as provisional

    The guard stops your code from applying one tick's contact repeatedly. It doesn't make every contact that a client reports a real one.

    When a snapshot arrives, Unity Netcode moves each replicated Rigidbody component to the position in that snapshot before it replays. That's a discontinuous move rather than a simulated one, so physics generates contacts from the new position. A client can therefore report a contact that the server's timeline never produced, which is most visible when a correction moves an object backward into something.

    Keep authoritative decisions on the server, and let the client predict only what it can correct:

    • Decide damage, scoring, and object destruction on the server, where each tick runs once and the state is authoritative.
    • Use client-side contacts for immediate feedback, such as an impact sound or a particle effect. A wrongly predicted contact produces a small amount of feedback that the server never confirms. Make a long-running animation or visual effect cancelable, or short enough that a wrong prediction isn't jarring.

    For more information about smoothing corrections, refer to Prediction error smoothing.

    Additional resources

    • Hybrid GhostObjects and entities in the same project
    • Write prediction logic in a GhostBehaviour
    • Introduction to prediction
    • Managing latency with prediction
    • Prediction error smoothing
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)