Prediction in GameObject Netcode - GhostObjects Released

With recent changes to Discussions, don’t forget to subscribe to the multiplayer product update category to get updated on our announcements!

We’re super excited to announce the release of the first iteration of our GhostObject APIs, available to both Netcode for Entities and Netcode for GameObjects along with a package rename!

These releases come with a slew of new multiplayer features, previously only available to Netcode for Entities flows and now available to GameObjects. This release also means that as of package version 7.2.0, Netcode for Entities is becoming Unity Netcode. The technical name of the package remains unchanged (com.unity.netcode).

This renamed version of the package includes the full release of the GhostObject API, allowing you to create real-time multiplayer games using GameObject workflows, while benefitting from Netcode for Entities’ features. Previously, if you wanted client prediction for dealing with lag and multiplayer performance in your Unity game, you had to use DOTS and ECS with Netcode for Entities. If you’re used to working with GameObjects, then this was a non-trivial shift in how you create games. Netcode for Entities has a spread of cool features like prediction, relevancy, importance scaling, delta compression, quantization, server rewind (lag compensation), and Bursted jobs for serialization that require using ECS and have been unavailable for use with GameObjects.

With this release, you can now attach GhostObjects to your GameObjects and add GhostBehaviours containing PredictionUpdate() methods, which interact with your rolled back/quantized/delta compressed GhostFields, to allow you to write predicted, scalable multiplayer code.

public partial class MyCharacter : GhostBehaviour
{
    GhostField<int> m_MyHealth;

    public override void PredictionUpdate(PredictionUpdateContext context)
    {
        var speed = 10;
        if (m_MyHealth.Value < 10) // rolled back to the appropriate tick
            speed = 2;
        transform.position.x += speed * context.TickedDeltaTime; // transforms are automatically rolled back.
        // Netcode.DeltaTime is also availble
    }
}

Those GhostFields rely on the same underlying implementation as Netcode for Entities, with quantization, delta compression, rollbacks, using the same jobified, Bursted serialization pipeline.

This is a small sample of what you can now do. For the full feature set, please see our documentation here. For those hearing about this for the first time, you can check what we’ve been up to in more detail in our dev log entries here.

To try this new version, download the latest Unity 6000.7.0 version and update Netcode to 7.2.0. To get all the performance advantages, make sure to enable burst (Jobs > Burst > Enable Compilation). There will be an initial compilation time, but following updates to managed code should be relatively quick, Burst will ignore those.

Package versions

Netcode for Entities package version 7.2.0 officially renames the package to Unity Netcode. Unity Netcode provides a base for the future of netcode in Unity. The technical name of the package remains unchanged (com.unity.netcode). It’s compatible with Unity 6000.7+ and contains the GhostObject APIs.

In version 3.0.0, Netcode for GameObjects introduced a dependency on Unity Netcode. The new 3.2.0 release of Netcode for GameObjects officially supports hybrid sessions where Netcode for GameObjects and Unity Netcode can be used together in the same session, and on the same GameObject.

Since our last dev log: Unity Netcode 7.2.0

Lots has happened since our last dev log entry. We’ve added features, we’ve stabilized things, we’ve renamed a bunch of APIs. You can read our docs here with a new section on GhostObjects. Samples are available here.

We’ll detail more in upcoming dev logs, but for now a few note-worthy items since the last dev log:

  • Unity 6000.7 includes Physics improvements to help predicting and rolling back Rigidbodies.

  • We added an option to enable delayed spawning (in addition to the normal direct GameObject instantiation flows).

  • Improved prefab management. Netcode now handles gracefully a client skipping loading a prefab.

  • Predicting and syncing parent changes.

  • Enabled nested GhostBehaviours and made a few changes to simplify Netcode in general like making partial ticks optional or automatically enabling state replication when connecting.

To improve general onboarding, this release also includes a few changes to our defaults. Please check out our changelog here and upgrade guide here for more details.

How this fits with Netcode for GameObjects

In parallel to this release, Netcode for GameObjects releases its 3.2.0 version, allowing you to use GhostObjects with your existing NetworkObjects. When you add both a GhostObject and a NetworkObject to the root of a GameObject, you can now remove your NetworkTransform and make use of the jobified, Bursted, and delta compressed transform syncing from GhostObject. This will allow you to save on CPU and bandwidth for your existing server-driven transform syncing, while keeping your existing NetworkObject based flows.

In a hybrid session, your NetworkManager sets up the Unity Netcode connection for your project, which means you connection flows should continue working, while Netcode for GameObjects shares the same connection with Unity Netcode. This does mean that the initial project setup differs based on whether you use Unity Netcode standalone, or in conjunction with Netcode for GameObjects and a NetworkManager.

For more information on how to port your existing project to these new features from previous Netcode for GameObjects versions, please refer to the doc here.

Next steps

We’ll continue work on performance and feature improvements. Our current to-do list includes object pooling, stabilizing CorePhysics2D support (hidden behind a define in 7.2.0), adding support for more 3D physics primitives like character controller and colliders, more RPC targeting capabilities, prediction error smoothing, relevancy APIs on top of ECS APIs, in-scene placed objects (aka prespawn), and utility callbacks like OnServerStarted and OnValueChanged for GhostFields.

Unity 7

We’ll also be looking at putting the two Netcode packages’ code bases in the same package in the Unity 7 timeline. This is NOT a deprecation of any one feature set, more an administrative first step to help with merging the two code bases.
With ECS for all, we’ll also look into handling the new Entities flows for Unity 7+. We’ll keep making our upcoming package releases for 6000.7+ and 7000.x+, compatible with both editor’s current way to handle ECS.

This is the first iteration and we need your feedback! We’ll continue improving this alongside you, please help us shape our next releases.

We’re incredibly excited to see what you will build with this!

15 Likes

Hey, thanks for the update.

How does it play along with physics? When does the PredictionUpdate happen?

Hey @mishakozlov74 PredictionUpdate() gets triggered from the ECS SimulationSystemGroup, which is right after MonoBehaviour Update()

For physics, you’ll want to use PredictionPhysicsUpdate(), also called from ECS update. It updates before netcode calls Physics.Simulate() and is in step with it.
With prediction, Netcode takes over the physics simulation and calls Simulate itself at the right rate. At tick rate server/host side and a prediction rate (so rollback + replay) client side.

So you have

  • MonoBehaviour FixedUpdate()
  • MonoBehaviour Update()
  • GhostBehaviour PredictionGatherInput() - once per frame
  • Stepped together multiple times:
    • GhostBehaviour PredictionPhysicsUpdate() - x times per frame, full ticks only
    • Physic.Simulate()
    • GhostBehaviour PredictionUpdate() - n times per frame, can have partial ticks/batched if enabled

Prediction runs linearly server side, there’s no replay, but can still tick more than once per frame.
Physics doc: Use physics with predicted GhostObjects | Unity Netcode | 7.2.0

You’re making me realize we should probably have a page on this in our doc :smiley:

2 Likes

Thanks, it’s clear now!

1 Like

I was wondering is there any plans to remove or improve the limitation of GhostObjects being set in the scene for Hybrid mode.

Looks like I forgot to write it in the list above: we’re working on in-scene placed objects support too!

1 Like

Was going to ask if Unity wants any test projects for any of these. I was an early tester for Unity Physics Core 2D before Unity 6.3 came out of alpha and have used it 100% since it came out on all projects and also helped test out the burst compiled tilemap while helping report bugs for it.

I actually have a multiplayer project with GhostFields and GhostObjects in Unity 7 alpha with Physics Core 2D API using PhysicsShape and PhysicsBody. If the team needs any feedback on the current state of it I would be happy to give it.

Also you already mentioned the improvements for in scene placed ghost objects, but was wondering is there any plan for Distributed Authority full support for Ghost Objects. Originally had the project running with Distributed Authority, but switched to client-server to test out GhostObjects and GhostFields.

Always happy to hear feedback! Our current physics 2D support is very early and is very likely going to change, just as an FYI (hence the define symbol). But it’s great you’re testing things out!

Also you already mentioned the improvements for in scene placed ghost objects, but was wondering is there any plan for Distributed Authority full support for Ghost Objects. Originally had the project running with Distributed Authority, but switched to client-server to test out GhostObjects and GhostFields.

We have plans for client authority support first. Distributed Authority is something that’s on our mind, but nothing to announce yet.

2 Likes

NetcodeSamples has been updated here

To get a version compatible with Netcode for Entities 1.x, please use the netcode/1-x-sample branch

1 Like