コード例 #1
0
    private void FixedUpdate()
    {
        if (client != null && client.Connected)
        {
            OnNetworkReceived();

            lastDistance = Vector3.Distance(lastNetworkedPosition, player.transform.position);
            if (lastDistance >= MIN_DISTANCE_TO_SEND_POSITION)
            {
                var message = new NetworkMessage();

                message.AddTagPacket(NetworkTagPacket.PlayerPosition);
                message.AddFloat(player.transform.position.x);
                message.AddFloat(player.transform.position.y);
                message.AddFloat(player.transform.position.z);

                client.Send(message.Buffer);

                lastNetworkedPosition = player.transform.position;
            }
        }

        foreach (var player in netPlayersDictionary)
        {
            if (!player.Value.GameObjectAdded)
            {
                player.Value.GameObjectAdded = true;
                player.Value.GameObject      = Instantiate(netPlayerPrefab, player.Value.Position, Quaternion.identity);
            }
            else
            {
                player.Value.GameObject.transform.position = player.Value.Position;
            }
        }
    }