private static void processRigidbodyMessage(byte[] msg, bool clientMessage) { short netID = ObjectSerializer.deserializeShort(ref msg); if (NetworkEntityManager.Instance.findComponent(netID, out NetworkTransform networkTransform)) //NetworkTransform found { bool receivedVelocity = ObjectSerializer.deserializeByte(ref msg) == 1; if (receivedVelocity) //Velocity data was sent { float velX = ObjectSerializer.deserializeFloat(ref msg); float velY = ObjectSerializer.deserializeFloat(ref msg); float velZ = ObjectSerializer.deserializeFloat(ref msg); networkTransform.Velocity = new Vector3(velX, velY, velZ); } bool receivedAngularVelocity = ObjectSerializer.deserializeByte(ref msg) == 1; if (receivedAngularVelocity) //Angular velocity data was sent { float aVelX = ObjectSerializer.deserializeFloat(ref msg); float aVelY = ObjectSerializer.deserializeFloat(ref msg); float aVelZ = ObjectSerializer.deserializeFloat(ref msg); networkTransform.AngularVelocity = new Vector3(aVelX, aVelY, aVelZ); } //Message was sent from a client, now being processed on the server if (clientMessage && ServerBehaviour.Instance) { short clientID = ObjectSerializer.deserializeShort(ref msg); //Client who sent the message byte[] rigidbodyMsg = MessageFactory.createRigidbodyMessage(networkTransform.networkIdentity, networkTransform.rb, receivedVelocity, receivedAngularVelocity); ServerBehaviour.Instance.sendMessage(rigidbodyMsg, clientID); //Forward to all clients but the sender } } }
//Updates the network with the new transform data private void updateNetwork(bool sendPosition, bool sendRotation, bool sendScale, bool sendVelocity, bool sendAngularVelocity) { if (hasNetworkAuthority) { bool clientMessage = networkIdentity.networkAuthority == NetworkAuthority.CLIENT; //Determines if the message should include the client ID //Sync transform data networkIdentity.sendMessage(MessageFactory.createTransformMessage(networkIdentity, transformSpace, sendPosition, sendRotation, sendScale, clientMessage)); if (syncMode == SyncMode.SyncRigidbody) { networkIdentity.sendMessage(MessageFactory.createRigidbodyMessage(networkIdentity, rb, sendVelocity, sendAngularVelocity, clientMessage)); } } }