public override void OnRPCCommand(NetworkPlayer player, string command) { base.OnRPCCommand(player, command); NetworkRPC rpc = NetworkRPC.FromString(command); switch (rpc.m_Type) { case NetworkRPCType.RPC_CUSTOM_PLAYER: { NetworkPlayerRPC playerRPC = NetworkRPC.Parse <NetworkPlayerRPC>(command); GameObject obj = NetworkManager.Instance.GetNetworkedObject(playerRPC.m_NetworkId); if (PlayerHasAuthority(obj, player)) { if (playerRPC.m_Health != -1) { ELogger.Log($"{playerRPC.m_NetworkId} is trying to edit their health to {playerRPC.m_Health}!", ELogger.LogType.Server); break; } playerRPC.m_Health = obj.GetComponent <Player>().m_Health; // Set it to the server health obj.GetComponent <Player>().UpdateColor(playerRPC.m_Color); UpdateRPC(obj, playerRPC); SendRPCAll(playerRPC); } } break; } }
private void Update() { if (m_IsClient) { if (m_HasAuthority) { if (m_Movement) { translation = Input.GetAxis("Vertical") * speed * Time.deltaTime; straffe = Input.GetAxis("Horizontal") * speed * Time.deltaTime; transform.Translate(straffe, 0, translation); } if (Input.GetKeyDown(KeyCode.F)) { m_Color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f)); NetworkPlayerRPC rpc = new NetworkPlayerRPC(m_Color, -1, m_Identity.m_NetworkId); // Health is -1 as it doesnt matter we we set it, only what the server sets NetworkClient.Instance.SendRPC(rpc); } if (Input.GetKeyDown(KeyCode.Escape)) { m_Movement = !m_Movement; } } } }
private void OnCollisionEnter(Collision collision) { if (m_IsServer) { // If a client calls this their game will probably just crash since NetworkServer.Instance isnt a thing GameObject collided = collision.gameObject; Player player = collided.GetComponentInParent <Player>(); if (player == null) { NetworkServer.Instance.DestroyNetworkedObject(m_Identity.m_NetworkId); return; } player.m_Health -= 25; NetworkPlayerRPC rpc = new NetworkPlayerRPC(player.m_Color, player.m_Health, m_Identity.m_NetworkId); rpc.m_Important = true; NetworkServer.Instance.UpdateRPC(player.gameObject, rpc); NetworkServer.Instance.SendRPCAll(rpc); NetworkServer.Instance.DestroyNetworkedObject(m_Identity.m_NetworkId); } if (m_IsClient) { // Spawn hit marker, bullet effect, etc } }
/// <summary> /// An optional way to receive any extra RPC commands. /// Can be used to update things like health, active gun, animation (maybe done by library later), etc /// Note, all calls are sent from the server never another client. So things like health can be checked via the server and not here /// </summary> /// <param name="content">The content of the rpc we received. See example below to understand more</param> public override void OnRPCCommand(string content) { base.OnRPCCommand(content); NetworkRPC rpc = NetworkRPC.FromString(content); switch (rpc.m_Type) { case NetworkRPCType.RPC_CUSTOM_PLAYER: { NetworkPlayerRPC playerRPC = NetworkRPC.Parse <NetworkPlayerRPC>(content); UpdateColor(playerRPC.m_Color); m_Health = playerRPC.m_Health; } break; } }