Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mortar networking assign owner server has authority by default

Discussion in 'Multiplayer' started by Defiled, Apr 29, 2018.

  1. Defiled

    Defiled

    Joined:
    Feb 10, 2014
    Posts:
    43
    Hey guy's I'm trying to create a mortar which only 1 client can control, these are my current codes.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class player : NetworkBehaviour
    7. {
    8.     public Transform mortar;
    9.  
    10.     public void Start()
    11.     {
    12.         mortar = GameObject.Find("Mortar").transform;
    13.  
    14.     }
    15.  
    16.     public void Update()
    17.     {
    18.         if (isLocalPlayer)
    19.         {
    20.             if (Input.GetKeyDown(KeyCode.E))
    21.             {
    22.                 Cmd_PressedE();
    23.             }
    24.         }
    25.     }
    26.  
    27.     [Command]
    28.     public void Cmd_PressedE()
    29.     {
    30.         mortar.GetComponent<mortar>().ChangeOwnership(connectionToClient);
    31.     }
    32. }
    33.  
    and my mortar script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class mortar : NetworkBehaviour
    7. {
    8.     public void ChangeOwnership(NetworkConnection id)
    9.     {
    10.         this.GetComponent<NetworkIdentity>().AssignClientAuthority(id);
    11.     }
    12.  
    13.     public void Update()
    14.     {
    15.         if (hasAuthority)
    16.         {
    17.             // runs on the client that has authority
    18.             // runs on the server always
    19.         }
    20.     }
    21. }
    22.  
    Now the issue I'm having is that the client that is hosting the server will always have 'hasAuthority' set to true by default, how do I make it so the server does not have authority by default?
     
  2. Defiled

    Defiled

    Joined:
    Feb 10, 2014
    Posts:
    43
    Or do I have to do it this way?
    Code (CSharp):
    1. if (hasAuthority && this.GetComponent<NetworkIdentity>().clientAuthorityOwner != null)
    2.         {
    3.             Debug.Log("has authority");
    4.             // runs on the client that has authority
    5.             // runs on the server always
    6.         }
    though
    this.GetComponent<NetworkIdentity>().clientAuthorityOwner is always null on client


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class mortar : NetworkBehaviour
    7. {
    8.     public void ChangeOwnership(NetworkConnection id)
    9.     {
    10.         this.GetComponent<NetworkIdentity>().AssignClientAuthority(id);
    11.     }
    12.  
    13.     public void Update()
    14.     {
    15.         if (isServer && this.GetComponent<NetworkIdentity>().clientAuthorityOwner == null)
    16.         {
    17.             return;
    18.         }
    19.  
    20.         if (isClient && !hasAuthority)
    21.         {
    22.             return;
    23.         }
    24.  
    25.         Debug.LogError("has authority");
    26.     }
    27. }
    28.  
    This might work, kinda hacky way of doing it though I feel like