public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { // I have control over the pieces, so I can move them and then send pos + rot over network if (stream.IsWriting) { stream.SendNext(MyFlippedCoordinates.PositionRelativeToBoard(Rb.position, Board)); stream.SendNext(Rb.isKinematic); stream.SendNext(Rb.rotation); stream.SendNext(Rb.velocity); } // No control over the object, so I update this class with the values read from the network // if (stream.IsReading) else { NetworkPositionRb = (Vector3)stream.ReceiveNext(); NetworkIsKinematic = (bool)stream.ReceiveNext(); NetworkRotation = (Quaternion)stream.ReceiveNext(); float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime)); Rb.velocity = (Vector3)stream.ReceiveNext(); NetworkPositionRb += Rb.velocity * lag; Distance = Vector3.Distance(Rb.position, NetworkPositionRb); } }
void SpawnPlayer(int i) { GameObject piece = IsFirstPlayer ? Instantiate(YellowPlayer, Positions[i].transform.position, PieceRotation) : Instantiate(RedPlayer, Positions[i].transform.position, PieceRotation); PhotonView view = piece.GetComponent <PhotonView>(); if (PhotonNetwork.AllocateViewID(view)) { object[] data = new object[] { MyFlippedCoordinates.PositionRelativeToBoard(piece.transform.position, Board), piece.transform.rotation, view.ViewID, IsFirstPlayer }; RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.Others, CachingOption = EventCaching.AddToRoomCache, }; SendOptions sendOptions = new SendOptions { Reliability = true }; PhotonNetwork.RaiseEvent((byte)RaiseEventCodes.PlayerSpawnEventCode, data, raiseEventOptions, sendOptions); } }
void FixedUpdate() { // I have no control over these pieces so retrieve network position and rotation // Need to add offset for other side of the board if (!PhotonView.IsMine) { transform.position = Vector3.MoveTowards( Rb.position, MyFlippedCoordinates.FlipPerspectiveOfBoardPiece(NetworkPositionRb, Board), Distance * (1.0f / PhotonNetwork.SerializationRate)); Rb.isKinematic = NetworkIsKinematic; Rb.rotation = NetworkRotation.normalized; } }
void OnEvent(EventData photonEvent) { if (photonEvent.Code == (byte)RaiseEventCodes.PlayerSpawnEventCode) { object[] data = (object[])photonEvent.CustomData; Vector3 receivedPosition = (Vector3)data[0]; Quaternion receivedRotation = (Quaternion)data[1]; int receivedViewID = (int)data[2]; bool receievedFirstPlayer = (bool)data[3]; GameObject player = receievedFirstPlayer ? Instantiate(YellowPlayer, MyFlippedCoordinates.FlipPerspectiveOfBoardPiece(receivedPosition, Board), receivedRotation) : Instantiate(RedPlayer, MyFlippedCoordinates.FlipPerspectiveOfBoardPiece(receivedPosition, Board), receivedRotation); PhotonView view = player.GetComponent <PhotonView>(); view.ViewID = receivedViewID; } }