public void copyFromSmoothSync(SmoothSyncPUN2 smoothSyncScript) { ownerTimestamp = smoothSyncScript.localTime; position = smoothSyncScript.getPosition(); rotation = smoothSyncScript.getRotation(); scale = smoothSyncScript.getScale(); if (smoothSyncScript.hasRigidbody) { velocity = smoothSyncScript.rb.velocity; angularVelocity = smoothSyncScript.rb.angularVelocity * Mathf.Rad2Deg; } else if (smoothSyncScript.hasRigidbody2D) { velocity = smoothSyncScript.rb2D.velocity; angularVelocity.x = 0; angularVelocity.y = 0; angularVelocity.z = smoothSyncScript.rb2D.angularVelocity; } else { velocity = Vector3.zero; angularVelocity = Vector3.zero; } localTimeResetIndicator = smoothSyncScript.localTimeResetIndicator; //atPositionalRest = smoothSyncScript.sendAtPositionalRestMessage; //atRotationalRest = smoothSyncScript.sendAtRotationalRestMessage; }
/// <summary> /// Deserialize a message from the network. /// </summary> /// <remarks> /// Only receives what it needs and decompresses floats if you chose to. /// </remarks> /// <param name="writer">The Networkreader to read from.</param> public void Deserialize(BinaryReader reader, SmoothSyncPUN2 smoothSync) { // The first received byte tells us what we need to be syncing. byte syncInfoByte = reader.ReadByte(); bool syncPosition = shouldSyncPosition(syncInfoByte); bool syncRotation = shouldSyncRotation(syncInfoByte); bool syncScale = shouldSyncScale(syncInfoByte); bool syncVelocity = shouldSyncVelocity(syncInfoByte); bool syncAngularVelocity = shouldSyncAngularVelocity(syncInfoByte); state.atPositionalRest = shouldBeAtPositionalRest(syncInfoByte); state.atRotationalRest = shouldBeAtRotationalRest(syncInfoByte); int syncIndex = (int)reader.ReadUInt32(); state.ownerTimestamp = reader.ReadSingle(); if (!smoothSync) { Debug.LogWarning("Could not find target for network state message."); return; } // If we want the server to relay non-owned object information out to other clients, set these variables so we know what we need to send. if (PhotonNetwork.IsMasterClient && !smoothSync.photonView.IsMine) { state.serverShouldRelayPosition = syncPosition; state.serverShouldRelayRotation = syncRotation; state.serverShouldRelayScale = syncScale; state.serverShouldRelayVelocity = syncVelocity; state.serverShouldRelayAngularVelocity = syncAngularVelocity; } // Find the correct object to sync according to the syncIndex. for (int i = 0; i < smoothSync.childObjectSmoothSyncs.Length; i++) { if (smoothSync.childObjectSmoothSyncs[i].syncIndex == syncIndex) { smoothSync = smoothSync.childObjectSmoothSyncs[i]; } } state.receivedTimestamp = smoothSync.localTime; if (smoothSync.receivedStatesCounter < PhotonNetwork.SerializationRate) { smoothSync.receivedStatesCounter++; } // Read position. if (syncPosition) { if (smoothSync.isPositionCompressed) { if (smoothSync.isSyncingXPosition) { state.position.x = HalfHelper.Decompress(reader.ReadUInt16()); } if (smoothSync.isSyncingYPosition) { state.position.y = HalfHelper.Decompress(reader.ReadUInt16()); } if (smoothSync.isSyncingZPosition) { state.position.z = HalfHelper.Decompress(reader.ReadUInt16()); } } else { if (smoothSync.isSyncingXPosition) { state.position.x = reader.ReadSingle(); } if (smoothSync.isSyncingYPosition) { state.position.y = reader.ReadSingle(); } if (smoothSync.isSyncingZPosition) { state.position.z = reader.ReadSingle(); } } } else { if (smoothSync.stateCount > 0) { state.position = smoothSync.stateBuffer[0].position; } else { state.position = smoothSync.getPosition(); } } // Read rotation. if (syncRotation) { state.reusableRotationVector = Vector3.zero; if (smoothSync.isRotationCompressed) { if (smoothSync.isSyncingXRotation) { state.reusableRotationVector.x = HalfHelper.Decompress(reader.ReadUInt16()); state.reusableRotationVector.x *= Mathf.Rad2Deg; } if (smoothSync.isSyncingYRotation) { state.reusableRotationVector.y = HalfHelper.Decompress(reader.ReadUInt16()); state.reusableRotationVector.y *= Mathf.Rad2Deg; } if (smoothSync.isSyncingZRotation) { state.reusableRotationVector.z = HalfHelper.Decompress(reader.ReadUInt16()); state.reusableRotationVector.z *= Mathf.Rad2Deg; } state.rotation = Quaternion.Euler(state.reusableRotationVector); } else { if (smoothSync.isSyncingXRotation) { state.reusableRotationVector.x = reader.ReadSingle(); } if (smoothSync.isSyncingYRotation) { state.reusableRotationVector.y = reader.ReadSingle(); } if (smoothSync.isSyncingZRotation) { state.reusableRotationVector.z = reader.ReadSingle(); } state.rotation = Quaternion.Euler(state.reusableRotationVector); } } else { if (smoothSync.stateCount > 0) { state.rotation = smoothSync.stateBuffer[0].rotation; } else { state.rotation = smoothSync.getRotation(); } } // Read scale. if (syncScale) { if (smoothSync.isScaleCompressed) { if (smoothSync.isSyncingXScale) { state.scale.x = HalfHelper.Decompress(reader.ReadUInt16()); } if (smoothSync.isSyncingYScale) { state.scale.y = HalfHelper.Decompress(reader.ReadUInt16()); } if (smoothSync.isSyncingZScale) { state.scale.z = HalfHelper.Decompress(reader.ReadUInt16()); } } else { if (smoothSync.isSyncingXScale) { state.scale.x = reader.ReadSingle(); } if (smoothSync.isSyncingYScale) { state.scale.y = reader.ReadSingle(); } if (smoothSync.isSyncingZScale) { state.scale.z = reader.ReadSingle(); } } } else { if (smoothSync.stateCount > 0) { state.scale = smoothSync.stateBuffer[0].scale; } else { state.scale = smoothSync.getScale(); } } // Read velocity. if (syncVelocity) { if (smoothSync.isVelocityCompressed) { if (smoothSync.isSyncingXVelocity) { state.velocity.x = HalfHelper.Decompress(reader.ReadUInt16()); } if (smoothSync.isSyncingYVelocity) { state.velocity.y = HalfHelper.Decompress(reader.ReadUInt16()); } if (smoothSync.isSyncingZVelocity) { state.velocity.z = HalfHelper.Decompress(reader.ReadUInt16()); } } else { if (smoothSync.isSyncingXVelocity) { state.velocity.x = reader.ReadSingle(); } if (smoothSync.isSyncingYVelocity) { state.velocity.y = reader.ReadSingle(); } if (smoothSync.isSyncingZVelocity) { state.velocity.z = reader.ReadSingle(); } } smoothSync.latestReceivedVelocity = state.velocity; } else { // If we didn't receive an updated velocity, use the latest received velocity. state.velocity = smoothSync.latestReceivedVelocity; } // Read anguluar velocity. if (syncAngularVelocity) { if (smoothSync.isAngularVelocityCompressed) { state.reusableRotationVector = Vector3.zero; if (smoothSync.isSyncingXAngularVelocity) { state.reusableRotationVector.x = HalfHelper.Decompress(reader.ReadUInt16()); state.reusableRotationVector.x *= Mathf.Rad2Deg; } if (smoothSync.isSyncingYAngularVelocity) { state.reusableRotationVector.y = HalfHelper.Decompress(reader.ReadUInt16()); state.reusableRotationVector.y *= Mathf.Rad2Deg; } if (smoothSync.isSyncingZAngularVelocity) { state.reusableRotationVector.z = HalfHelper.Decompress(reader.ReadUInt16()); state.reusableRotationVector.z *= Mathf.Rad2Deg; } state.angularVelocity = state.reusableRotationVector; } else { if (smoothSync.isSyncingXAngularVelocity) { state.angularVelocity.x = reader.ReadSingle(); } if (smoothSync.isSyncingYAngularVelocity) { state.angularVelocity.y = reader.ReadSingle(); } if (smoothSync.isSyncingZAngularVelocity) { state.angularVelocity.z = reader.ReadSingle(); } } smoothSync.latestReceivedAngularVelocity = state.angularVelocity; } else { // If we didn't receive an updated angular velocity, use the latest received angular velocity. state.angularVelocity = smoothSync.latestReceivedAngularVelocity; } //// Update new owner information sent from the Server. //if (smoothSync.isSmoothingAuthorityChanges && !NetworkServer.active) //{ // smoothSync.ownerChangeIndicator = (int)reader.ReadByte(); //} if (smoothSync.automaticallyResetTime) { state.localTimeResetIndicator = (int)reader.ReadByte(); } }