public override void Serialize(NetworkWriter writer) { bool sendPhysics = this.rigidBody != null; bool useGravity = sendPhysics && this.rigidBody.useGravity; bool freezeRotation = sendPhysics && this.rigidBody.freezeRotation; bool isKinematic = sendPhysics && this.rigidBody.isKinematic; int sent = 0; NetworkUtil.SetBit(ref sent, 0, this.sendPosition); NetworkUtil.SetBit(ref sent, 1, this.sendRotation); NetworkUtil.SetBit(ref sent, 2, this.sendScale); NetworkUtil.SetBit(ref sent, 3, sendPhysics); NetworkUtil.SetBit(ref sent, 4, useGravity); NetworkUtil.SetBit(ref sent, 5, freezeRotation); NetworkUtil.SetBit(ref sent, 6, isKinematic); writer.WritePackedUInt32(++this.version); writer.WritePackedUInt32((uint)sent); if (this.sendPosition) { writer.Write(NetworkTransformAnchor.InverseTransformPosition(this.transform.position)); } if (this.sendRotation) { writer.Write(NetworkTransformAnchor.InverseTransformRotation(this.transform.rotation)); } if (this.sendScale) { writer.Write(this.transform.localScale); } if (sendPhysics) { writer.Write(this.rigidBody.velocity); } }
public override void Deserialize(NetworkReader reader) { uint messageVersion = reader.ReadPackedUInt32(); if (this.version > messageVersion) { return; } this.version = messageVersion; int sent = (int)reader.ReadPackedUInt32(); bool sentPosition = NetworkUtil.GetBit(sent, 0); bool sentRotation = NetworkUtil.GetBit(sent, 1); bool sentScale = NetworkUtil.GetBit(sent, 2); bool sentPhysics = NetworkUtil.GetBit(sent, 3); bool useGravity = NetworkUtil.GetBit(sent, 4); bool freezeRotation = NetworkUtil.GetBit(sent, 5); bool isKinematic = NetworkUtil.GetBit(sent, 6); if (sentPosition) { Vector3 newDesiredPosition = NetworkTransformAnchor.TransformPosition(reader.ReadVector3()); if (this.lastVelocityUpdateTime < 0.0f) { this.estimatedVelocity = Vector3.zero; } else { float timeSinceLastUpdate = Time.realtimeSinceStartup - this.lastVelocityUpdateTime; if (timeSinceLastUpdate > 0.01) { this.estimatedVelocity = (newDesiredPosition - this.desiredPosition) / timeSinceLastUpdate; } } this.desiredPosition = newDesiredPosition; this.lastVelocityUpdateTime = Time.realtimeSinceStartup; } if (sentRotation) { this.desiredRotation = NetworkTransformAnchor.TransformRotation(reader.ReadQuaternion()); } if (sentScale) { this.desiredScale = reader.ReadVector3(); } // Early out and don't update physics if we're trying to get ownership of this object if (this.Identity.IsRequestingOwnership) { return; } if (sentPhysics) { this.rigidBody.velocity = reader.ReadVector3(); this.rigidBody.useGravity = useGravity; this.rigidBody.freezeRotation = freezeRotation; this.rigidBody.isKinematic = isKinematic; } }