- Home /
Photon Unity Networking carrying scene objects
I am currently working on an online 2D game where players have to pick up a ball and run around with it. They can also throw it on targets in order to score points or so that other teammates can catch it.
As of now, when a player touches this ball with the arm of his character, it will stick to it and follow him wherever he goes. This is done using the following code in the update function of the arm:
if (isHolding == true && ToBePickedUp.isHeld == true)
{
target.transform.position = transform.position;
}
isHeld is a public static bool contained in a script attached to the ball object. Whenever the object moves into a player's control zone, the boolean is changed via Remote Procedure Call. I used Debug.Log to make sure this value is changed correctly. This variable is there to let other player instances know the ball is currently held.
When the master client catches the ball, he can run around with it and throw it without any problem. The changes will even be perceived on other players' screens. However, when another player tries to catch or even move the ball around (by running into it with his collider) nothing happens on other players' screens. The ball can ricochet of them if they are in its path however. I'm using the OnPhotonSerializeView function to follow the ball's movement.
This first image shows the view on the green player's screen (red player is the master client).
The second one shows the view on the red player's screen.
When the green player releases the ball, it immediately returns to the position on red player's screen. The network manager has been largely inspired by the one showed on quill18's tutorial. I have looked at the DemoPickup from the Photon package but could not find an answer. I also looked on unity answers before posting. This investigation led me to think some missing RPC prevents the network from following the ball when it's position is modified by the green player. I spent hours trying to find where to put it without success so I'm begenning to think this might not be the issue after all.
Any help would be much appreciated!
As side note: I have a hard time finding the essence of your post. What exactly you can't solve or want answered.
Answer by tobiass · Dec 02, 2014 at 05:32 PM
The owner of a PhotonView is the one who writes updates in OnPhotonSerializeView.
The trick in the Demo Pickup (in the PUN package) is that the pickup-items don't really move. The droppable item has a special RPC that sets the position when it gets dropped at some place but otherwise the object doesn't move.
The problem in your case is "ownership". The player who Instantiates the ball will own it. If it's a scene object (loaded with the scene but having a PhotonView), the Master Client owns the object.
You want one player to pick up the item, move it and this player can decide where the object is.
PUN 1.28 doesn't allow you to take control of objects this way. This is a feature in the upcoming release.
You could try to work around the problem by not synchronizing the position in OnPhotonSerializeView. Instead use RPCs. You know who has the ball and who can shoot it. The player who is in control might send RPCs to shoot the ball and to update the other.
Hello Tobiass,
Thank you for taking the time to answer my question! I realize I might not have been clear on what I wanted solved, but you seem to have grasped my problem quite well juging by your answer.
From what I can understand, the best way for me to work out the problem would be to destroy the ball object when it's picked up using RPC's and have the player who did it instanciate a new ball when he shoots. Is that what you meant in your last paragraph?
Also, since only the $$anonymous$$aster Client owns scene objects, could it be a good idea to make the player who picks up the ball the $$anonymous$$aster Client or the previous $$anonymous$$aster Client would still be considered the one who instanciated the object?
Finally, you mention the feature I'm looking for will be in the upco$$anonymous$$g release. Is there an estimated time of arrival for this release?
Thanks again for your time, your answer is already very helpful!
The new feature is in PUN v1.50 and up (in the Asset Store).