//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));
                }
            }
        }
示例#2
0
        private static void processTransformMessage(byte[] msg, bool clientMessage)
        {
            short netID = ObjectSerializer.deserializeShort(ref msg);

            if (NetworkEntityManager.Instance.findComponent(netID, out NetworkTransform networkTransform)) //NetworkTransform found
            {
                bool receivedPos = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedPos) //Position data was sent
                {
                    float posX = ObjectSerializer.deserializeFloat(ref msg);
                    float posY = ObjectSerializer.deserializeFloat(ref msg);
                    float posZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Position = new Vector3(posX, posY, posZ);
                }

                bool receivedRot = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedRot) //Rotation data was sent
                {
                    float rotX = ObjectSerializer.deserializeFloat(ref msg);
                    float rotY = ObjectSerializer.deserializeFloat(ref msg);
                    float rotZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Rotation = Quaternion.Euler(new Vector3(rotX, rotY, rotZ));
                }

                bool receivedScale = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedScale) //Scale data was sent
                {
                    float scaleX = ObjectSerializer.deserializeFloat(ref msg);
                    float scaleY = ObjectSerializer.deserializeFloat(ref msg);
                    float scaleZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Scale = new Vector3(scaleX, scaleY, scaleZ);
                }

                //Message was sent from a client, now being processed on the server
                if (clientMessage && ServerBehaviour.Instance)
                {
                    //What transform space is this data in? (World / Local)
                    bool  worldSpace = ObjectSerializer.deserializeByte(ref msg) == 1;
                    short clientID   = ObjectSerializer.deserializeShort(ref msg); //Client who sent the message
                    NetworkTransform.TransformSpace transformSpace = worldSpace ? NetworkTransform.TransformSpace.World : NetworkTransform.TransformSpace.Local;
                    byte[] transformMsg = MessageFactory.createTransformMessage(networkTransform.networkIdentity, transformSpace, receivedPos, receivedRot, receivedScale);
                    ServerBehaviour.Instance.sendMessage(transformMsg, clientID); //Forward to all clients but the sender
                }
            }
        }