PhotonUnity player move object problem !
Hey, sorry for my bad english. here's my problem.... now I'm try to make Multiplayer Stuff with Photon Unity network right now, I can spawn a player and each player can't see each other next I'm try to let the player instantiate a Cube from the scripts ( not a prefab ) and move them [PunRPC] public void CreateObj(int objID) { id1 = PhotonNetwork.AllocateViewID(); if (objID == 1) { sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere); sphere.AddComponent<PhotonView> (); sphere.AddComponent<NetworkCharacter> (); PhotonView pv = sphere.GetComponent<PhotonView> (); pv.synchronization = ViewSynchronization.UnreliableOnChange; pv.ObservedComponents = new List<Component>(); pv.ObservedComponents.Add(sphere.GetComponent<NetworkCharacter> ());
the problem is each player can Move them BUT each player can see another player's object movement which mean the Object that have been spawn remain the same position in another player screen
I tried to do few things and i assume that I have to used 2 method which is RPCs to send a new Position of that object look like this
[PunRPC]
void OnGizmo(int selectID, float AmountToMove)
{
Debug.Log (selectID +","+AmountToMove );
target.transform.Translate (axis * AmountToMove, Space.World);
// PhotonView.Find(selectID).transform.Translate (axis * moveAmount, Space.World);
PhotonView.Find(selectID).transform.position = new Vector3(target.transform.position.x,target.transform.position.y,target.transform.position.z);
}
and another method by 'void OnPhotonSerializeView'
void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info)
{
Debug.Log ("isStream");
if (stream.isWriting) {
Debug.Log ("isWriting");
stream.SendNext (transform.position);
stream.SendNext (transform.rotation);
} else {
Debug.Log ("isReading");
FirstPosition = (Vector3)stream.ReceiveNext ();
realRotation = (Quaternion)stream.ReceiveNext ();
}
}
which I already attached to Observe Components list BUT stream.isWriting are not firing after I tried to Debug it and I don't sure why this happen and how to fix it,
Anyone have any suggestion PLEASE let me know I really need you guys, THANKS!
Answer by ChristianSimon · Mar 13, 2018 at 10:29 AM
Hi,
I don't know if this already solves the problem but in your CreateObj function you are calling id1 = PhotonNetwork.AllocateViewID();
. Since this is marked as [PunRPC] I assume that each client calls this line which is not the correct way of doing it because each client will allocate a different ViewId this way. If you want to use Manual Instantiation, I would recommend you taking a look at the Manual Instantiation section (bottom of the page) of the this documentation page.
Besides that in your OnPhotonSerializeView function the receiver just stores received information in local variables 'FirstPosition' and 'RealRotation'. If you update the object's transform component anywhere else in your code, it's fine. If you don't update it, you have to do it somewhere in your code.
Your answer
Follow this Question
Related Questions
Help with photon being used to make a mulitplayer fps 0 Answers
Syncing live animations using PUN 2 Answers