- Home /
Photon synchronization
Im having a hard time synchronizing weapons for my character. I have a GameObject "WeaponAttacher" that holds the position and rotation for the weapon, it is a child of the hand bone. When i equip a weapon i set the weapon gameobjects rotation and position to the attachers and i parent it to the attacher gameobject. I dont get what i need to synchronize for other clients to see.
WeaponSlot = (GameObject)PhotonNetwork.Instantiate (name, WeaponAttacher.position, WeaponAttacher.rotation, 0);
WeaponSlot .GetComponent<DroppedItem> ().item = itemDatabase.getNewItem(id);
WeaponSlot .transform.parent = WeaponAttacher;
EquippedWeaponSlot = WeaponSlot ;
The EquippedWeaponSlot is just a reference to the currently equipped weapon, as i have 2 slots for weapons.
The weapon is instantiated in the right place, its just not following the movement it should.
This is the syncronization code:
void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info) {
if (stream.isWriting) {
stream.SendNext (WeaponAttacher.transform.rotation);
stream.SendNext (WeaponAttacher.transform.position);
stream.SendNext (WeaponSlot.transform.position);
stream.SendNext (WeaponSlot.transform.rotation);
stream.SendNext (transform.Find("metarig/hips/spine/chest/shoulder.L/upper_arm.L/forearm.L/hand.L").position);
stream.SendNext (transform.Find("metarig/hips/spine/chest/shoulder.L/upper_arm.L/forearm.L/hand.L").rotation);
} else {
WeaponAttacherRecievedRotation = (Quaternion) stream.ReceiveNext();
WeaponAttacherRecievedPosition = (Vector3) stream.ReceiveNext();
WeaponSlotRecievedRotation = (Quaternion)stream.ReceiveNext ();
WeaponSlotRecievedPosition = (Vector3)stream.ReceiveNext();
handBonePosition = (Vector3) stream.ReceiveNext();
handBoneRotation = (Quaternion) stream.ReceiveNext();
}
}
and this is started as a coroutine from the start method after a check if !photonview.ismine:
IEnumerator UpdateData () {
while (true) {
WeaponAttacher.transform.rotation = WeaponAttacherRecievedRotation;
WeaponAttacher.transform.position = WeaponAttacherRecievedPosition;
WeaponSlot.transform.rotation =WeaponSlotRecievedRotation;
WeaponSlot.transform.position = WeaponSlotRecievedPosition;
transform.Find("metarig/hips/spine/chest/shoulder.L/upper_arm.L/forearm.L/hand.L").position = handBonePosition;
transform.Find("metarig/hips/spine/chest/shoulder.L/upper_arm.L/forearm.L/hand.L").rotation = handBoneRotation;
yield return null;
}
}
Okay, let's start with narrowing down the problem.
First, do you have a photonView that is observing the script that has your OnSerializeView method?
Two, check if your OnSerializeView method is receiving data correctly (simply send some value and print it when you receive it, check if you get the same value)
Three, check if your UpdateData() method is firing, and also if it is using the correct data. (You can test this basically the same way as in 2, just have some value that you change over the network, then check if it's actually changing in your client).
Four, check when you equip a weapon with the top part of your code if those assignments happen properly on both clients.
Can't think of any other place your problem could be right now, so let's figure out exactly where in the pipeline it appears, then we can find a solution.
ok thank you, 1, the player has a photonview attached that is observing the script. 2, i sendt a vector3 and printed it upon recieve, it worked. 3, i tried sending a changing value and printing it in the UpdateData, it didnt work, and i get errors:
PhotonView with ID 1001 has no method "InstantiateRanged" that takes 3 argument(s): int32, string, int32. and The observed monobehaviour (player(clone)) of this photonview does not implement OnPhotonSerializeView()!
PhotonView with ID 1001 has no method "InstantiateRanged" that takes 3 argument(s): int32, string, int32.
That means you're calling a method InstantiateRanged(int, string, int) somewhere that doesn't exist, most likely because your method is something like InstantiateRanged(int, string), i.e. the number and/or types of arguments don't match.
The observed monobehaviour (player(clone)) of this photonview does not implement OnPhotonSerializeView()!
This error means that you have a photonView somewhere that is observing a script, but that script has no OnSerializeView() method. Which makes observing that script a bit pointless, since no data gets sent or recieved in that case.
Somewhat strange that you didn't get those errors before, though, it seems like you should have. Still, it's a starting point.
I figured those errors out, the method arguments where wrong and i had put another script by mistake to be observed by the photonview.
All right, so now that you have resolved those, do you get a correct updated value in your UpdateData() method?
Answer by Mickwa · Mar 21, 2015 at 10:34 PM
Is order in OnPhotonSerializeView of stream.SendNext() and stream.ReceiveNext() correct? You send position than rotation:
stream.SendNext (WeaponSlot.transform.position);
stream.SendNext (WeaponSlot.transform.rotation);
and You should read in the same order as you sent position than rotation:
WeaponSlotRecievedPosition = (Vector3)stream.ReceiveNext();
WeaponSlotRecievedRotation = (Quaternion)stream.ReceiveNext();
Dont know how i missed that, looked over the sccript so many times, thank you. It didnt solve my problem tho
oh my god i just figured it out.. i have another script that disables all character scripts if the photonview is not $$anonymous$$e. So this script was allways disabled.
This isn't true. You deserialize in the same order that you serialize. Not in reverse order.
@DRRosen3, I updated my comment, thanks for pointing out that it was not valid any more!
@$$anonymous$$ickwa Hmm...I'm fairly new to PUN (less than a year experience) so I wasn't aware that in the past your original post was true. In any case, glad we got it straightened out for future readers!
Your answer
Follow this Question
Related Questions
How can I synchronize Instantiated children of a GameObject over Photon? 1 Answer
Sync game objects across network Photon C# 1 Answer
Snychronize player's name over Photon? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers