How do you sync a client's server time with the server's time?

From using lag simulation, I learned that the client’s time is always ahead or behind the server’s time.

As a small example, suppose the delay is 1 second.

If I ask the server for the time, it’ll tell me that the server is 60 seconds in.
If I ask the client for the client time (NetworkManager.LocalTime.Time), it’ll tell me that the server is 61 seconds in.
If I ask the client for the server time (NetworkManager.ServerTime.Time), it’ll tell me that the server is 59 seconds in.

How do I sync the client’s time, such that both the client and server say that the server is 60 seconds in?

ORIGINAL TITLE:
When casting a spell, or spawning an enemy in the future, how do you synchronize the event between players?

ORIGINAL POST:
In netcode for gameobjects, how do we make sure a future event happens to all players at the same time?

Conceptually, one of the ways to handle latency is to queue actions in the future rather than them handling immediately.

Suppose the delay is 0.5 seconds in the example, 2 examples are:

Suppose a player intends to cast a fireball client side in 2 seconds (animation/casting bar). On the server, we know it’ll be cast in 1.5 seconds (due to the 0.5 seconds latency). For other players, it’ll be cast in 1 second.

Suppose the server intends to spawn an enemy in 5 seconds. On the player side, it would spawn in 4.5 seconds due to latency.

I’ve been playing around with LocalTime and ServerTime. I can’t think of a good method that is resilient against latency and jitter. How do we typically handle this in netcode for gameobjects?

Nothing short of quantum networking you will never have perfect synchronization between clients and server, there’s too many variables like you mentioned this includes networking as well as machine hardware and software components.

Even if you send a timestamp from a server to a client over time there will be a difference, my suggestion is just use a (NTP) package if you’re really worried: GitHub - disas69/Unity-NTPTimeSync-Asset

But a simple fix is just to update the server client timestamp when you want.

But realistically, unless you are doing some high preformant game most of this wouldn’t really matter for a indie game, if a fireball is off by a second does it really matter?

If you are trying to prevent some form of date manipulation, you could test using a UTC call, at random intervals that the server/client agree within a “reasonable” amount that the time is the time… and disconnect if not… However, as has been said they will never be spot on. Hence you have to make some allowance.

Thanks everyone.

The NTP protocol would probably be the answer if we want as accurate of a time as possible.

The true answer would be for most people, having a perfectly synced time versus a 0.1 second offset makes very little difference.

often you need an admin level of privlage to change system time. However, verifying time and making a comparison to determine it hasnt been tampered with is not a bad idea.

Yeah, I was talking about more of the NetworkManager time rather than the person’s computer time.

Knowing when certain events are taking place is super useful for anticheat.