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

Client Authority takes away input

Discussion in 'Multiplayer' started by LDawson97, Nov 6, 2018.

  1. LDawson97

    LDawson97

    Joined:
    Aug 16, 2018
    Posts:
    19
    Hi,

    I am building a LAN game, but debugging/developing on a single machine at the moment. I have the following problem:

    If I use NetworkServer.SpawnWithClientAuthority(), only the client which hosts the server can move their own object. When i build the project and try to connect to that client, I can't give any input to my own object). However, if I just use NetworkServer.Spawn(), both clients have the correct input and can move their own objects.

    here is a description of my project:
    I have a Network Manager with the NM script and the NM HUD component, as well as a spawnable prefab named PlayerObject.

    PlayerObject has a script called PlayerManager.cs which spawns a prefabed object stored locally in the script as a GameObject called MyPlayerUnit.

    If I use NetworkServer.SpawnWithClientAuthority(MyPlayerUnit, connectionToClient), the client which connects from a build (not the server), can't move it's own object.

    If I used NetworkServer.Spawn(MyPlayerUnit), the clients can move their objects appropriately, as I expect them to.

    I'm not sure why this problem exists, as each object should belong to their own clients when I use NetworkServer.SpawnWithClientAuthority().

    Here are screenshots to help:



    You can see the screenshots larger here:
    https://ibb.co/nOqtCq
    https://ibb.co/diMN5A
    https://ibb.co/kEyPJV
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You haven't shown enough of your code to say what the issue is. Post code in the forums directly using CODE tags, rather than using images of a few lines.
     
    LDawson97 likes this.
  3. LDawson97

    LDawson97

    Joined:
    Aug 16, 2018
    Posts:
    19
    Sure, here is the script which handles the spawning of the objects and assigning authority.
    I have changed the code so that it is now broken by changing NetworkServer.Spawn() to NetworkServer.SpawnWithClientAuthority().

    PlayerManager.cs
    - Attached to the Players object when they connect to the server

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class PlayerManager : NetworkBehaviour {
    8.  
    9.     public GameObject PlayerPrefab;
    10.     public List<GameObject> MyUnits = new List<GameObject>();
    11.     public float InputX, InputY;
    12.  
    13.     void Start()
    14.     {
    15.         // Is this my actual local player object?
    16.         if (isLocalPlayer == false)
    17.         {
    18.             return;     // This object is not ours! Don't run code.
    19.         }
    20.      
    21.         // Since the player object is invisible and not part of the world
    22.         //give me something physical to move aroundby asking the server to spawn our unit
    23.         CmdSpawnMyUnit();   Debug.Log("Spawning my own personal unit!");
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.         // Remember, update runs on everyones computer whether the own this object or note
    29.         if (isLocalPlayer == false)
    30.         {
    31.             return;
    32.         }
    33.  
    34.         // Take Input from local Player
    35.         InputX = Input.GetAxis("Horizontal") * 300 * Time.deltaTime;
    36.         InputY = Input.GetAxis("Vertical") * 300 * Time.deltaTime;
    37.      
    38.         // Ask the server to perform the movement
    39.         if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D)) CmdUnitMove(InputX);
    40.         if (Input.GetKeyDown(KeyCode.Space)) CmdUnitMoveUp();
    41.  
    42.         // Spawn more units if we like
    43.         if (Input.GetKeyDown(KeyCode.K))
    44.         {
    45.             CmdSpawnMyUnit();
    46.             Debug.Log("Spawning my own personal unit!");
    47.         }
    48.     }
    49.  
    50.     // Commands - Special functions only executed on the server
    51.     [Command] void CmdSpawnMyUnit()
    52.     {
    53.         // We are gaurenteed to be on the server in this method
    54.         GameObject go = Instantiate(PlayerPrefab);  Debug.Log("SERVER: Instantiated " + go.name);
    55.  
    56.         // Now that the object exists on the server, propagate to all clients and wire up the network identity
    57.         //NetworkServer.Spawn(go);
    58.         NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
    59.  
    60.         Debug.Log("SERVER: Spawned " + go.name);
    61.      
    62.         MyUnits.Add(go);
    63.     }
    64.  
    65.     [Command] void CmdUnitMove(float f)
    66.     {
    67.         foreach (var Unit in MyUnits)
    68.         {
    69.             if (Unit == null) return;
    70.             Unit.transform.Translate(f, 0, 0); Debug.Log("SERVER: Moved " + Unit.name);
    71.         }
    72.     }
    73.  
    74.     [Command] void CmdUnitMoveUp()
    75.     {
    76.         foreach (var Unit in MyUnits)
    77.         {
    78.             if (Unit == null) return;
    79.             Unit.transform.Translate(0, 1, 0); Debug.Log("SERVER: Moved " + Unit.name);
    80.         }
    81.     }
    82. }