We wrap up this series with an implementation of our theoretical solution to synchronizing items.
This article is part of the series starting with Synchronization Issues (Unity + Photon): Part 1.
We will create a wall (as before) to abstractly represent the paddle controlled by the second application (right). In the first game (left), we will make the wall gray and disable the collider; we want the second application be authoritative about collisions between the ball and the wall.
note: At this point, we expect the balls to be out-of-synch.
Re-Synchronizing the Balls
The general idea is that we want to detect the collision between the ball and the wall (paddle) in the second game and send the information to the first game to re-synchronize the ball.
We add a Photon View to the Side prefab and add the following script.
With this in place, the applications behave as such:
- The position of the balls are synchronized.
- We do see an artifact in the first game; the ball appears to penetrate the wall and then quickly jumps
- If the ball were to hit really close to where the wall and a side meet, we would have a problem because we be positioning the ball in the first (left) application inside the wall. This would be truly a corner-case (SMILE).
note: The only solution to the corner-case I could think of, is to break up the sides (top and bottom) into pieces and have the small pieces close to the wall (paddle) have the same treatment as the wall.
Hiding the Artifact
Struggled a bit to figure out how to hide the artifact, as it requires obscuring the ball and wall before the collision happens. One simple solution is to temporarily display a semi-transparent game object over the area of the artifact before the collision.
In this example, we create a Cube game object (turning it into a prefab, Obscure, in the Resources folder):
- We position it as to cover the wall and space immediately around it
- We set the Material to a semi-transparent material
- We set the Box Collider to be Is Trigger; prevents collisions but triggers events
- We apply the following script to it; shows and hides it
We then update GameManager.cs to instantiate the Obscure prefab on the first (left) application:
With this in place the applications behave as such; with the artifact mostly obscured.
Hope you found this helpful.