Synchronization Issues (Unity + Photon): Part 2
We make our first stab at synchronizing the ball using Photon components.
This article is part of the series starting with Synchronization Issues (Unity + Photon): Part 1.
Photon Transform View
The Photon tutorial uses a Photon Transform View to synchronize the position of the players
This class helps you to synchronize position, rotation and scale of a GameObject. It also gives you many different options to make the synchronized values appear smooth, even when the data is only send a couple of times per second. Simply add the component to your GameObject and make sure that the PhotonTransformView is added to the list of observed components
— Photon — PhotonTransformView Class Reference
Let us start by using this approach. Simply add a Photon Transform View to the Ball prefab and assign it as an Observed Component on the Ball’s Photon View component.
With this in place, the behavior looks fairly synchronized.
Observations:
- The second player (right) essentially sees the same thing as the first player (left) however a bit delayed.
- While the documentation is not clear on this, the behavior would suggest that the left application, that instantiated the ball, is controlling the position of the ball in the right application.
Photon Rigidbody View
This class helps you to synchronize the velocities of a physics RigidBody. Note that only the velocities are synchronized and because Unitys physics engine is not deterministic (ie. the results aren’t always the same on all computers) — the actual positions of the objects may go out of sync. If you want to have the position of this object the same on all clients, you should also add a PhotonTransformView to synchronize the position.
— Photon — PhotonRigidbodyView Class Reference
From the documentation, it seemed like the correct practice (in this case) to also add a Photon Rigidbody View component and add it as an Observed Component.
note: This had no visible impact on how the synchronization appeared in this example.
Network Lag
Let us consider how network lag, aka, ping or RTT (round-trip-time), can affect the synchronization of the position of our ball.
Many people assume that a fast, responsive internet connection relies solely on a good download and upload speed, but there’s more to it than that. There’s also ping, which is essentially a reaction time. If you have a ping of 98ms (milliseconds) that’s the time it has taken for your computer to respond to a request by another computer.
…
Anything below a ping of 20ms is considered to be great, while anything over 150ms could result in noticeable lag.
— Tech Advisor — How to lower your ping
We can use the Photon Lag Simulation Gui component, say adding it to the GameManager game object to test the application under varying network conditions.
Looks like my home’s 151ms RTT is just marginal.
Thinking about how Photon works, each application is communicating with the Photon servers, it actually takes the sum of both player’s RTT for information to go between applications; e.g., 300ms. So, looking back at our first attempt at synchronizing the balls’ movement we can now better understand the delay.
In both instances of the application, I enable the simulated lag (both applications’ RTT is around 340ms). With the combined delay being around 700ms, we are close to a full second between information being communicated between applications.
Observations:
- The movement of the ball on the left (instantiated the ball) is as expected.
- The movement of the ball on the right is delayed as before (but noticeably more)
- The movement of the ball on the right, also, is jerky. In some cases, it does not appear to even hit the wall.
Next Steps
In the next article, Synchronization Issues (Unity + Photon): Part 3, we explore some more fundamental problems that network lag can pose.