Search Unity

network transform component on non-player

Discussion in 'Multiplayer' started by Lelon, Jul 12, 2015.

  1. Lelon

    Lelon

    Joined:
    May 24, 2015
    Posts:
    79
    I managed to get prefabs spawn working correctly both on clients and host. But I have one problem now.
    I have a prefab with network identity that I spawned using the network.spawn method, this prefab have a network transform but it does not update the position if its not running on the host.
    I already have a player object with local authority, and I tried activating local authority on the spawned prefab too, but still same problem.
    The prefab position is controlled by the client player, however the network transform does not take care of the update delivery.
     
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    I also have a lot of trouble with the NetworkTransform component. Already reported about 4 bugs about it. Seriously considering rolling out my own component for this just because it is so filled with absolute showstopping bugs and weirdness. It is the only component that I really keep getting problems with though. The rest of UNET seems to work perfectly fine.

    I would really like to see Unity branch their patches from 5.2beta into 5.1 and see if that clears up some of these bugs.
     
  3. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Your object has to have local authority. You have to add the object on the server with NetworkServer.AddPlayerForConnection (),so that the client gets authority.
     
  4. Lelon

    Lelon

    Joined:
    May 24, 2015
    Posts:
    79
    Thank you for your reply. Can I use AddPlayerForConnection even tho the same client already have a player?
    I'm trying to get the player's pet to have its transform synchronized, the pet and the player are both in the same client instance, and the player controls the pet position client side.
     
  5. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    If you have a PRO license and can access the Unity 5.2 beta builds then they added this in 5.2b1

    • Networking: Added support for client-side authority for non-player objects.
      The new function NetworkServer.SpawnWithClientAuthority(GameObject obj, NetworkConnection conn) allows authority to be assigned to a client by its connection when an object is created. This would typically be used in a command handler for a client asking to spawn an object, then the client's connection would be passed in. For example:
      [Command]
      void CmdSpawn()
      {
      var go = (GameObject)Instantiate(otherPrefab, transform.position + new Vector3(0,1,0), Quaternion.identity);
      NetworkServer.SpawnWithClientAuthority(go, base.connectionToClient);
      }
      For setting the authority on objects after they are created, there are the new functions AssignClientAuthority(NetworkConnection conn) and RemoveClientAuthority(NetworkConnection conn) on the NetworkIdentity class. Note that only one client can be the authority for an object at a time. On the client that has authority, the function OnStartAuthority() is called, and the property hasAuthority will be true.
      The set of objects that is owned by a client is available in the new property NetworkConnection.clientOwnedObjects which is a set of NetworkInstanceIds. This set can be used on the server when a message is received to ensure that the client that sent the message actually owns the object.
      When a client disconnects, the function DestroyPlayersForConnection now destroys all the objects owned by that connection, not just the player object. Objects which have their authority set to a client must have LocalPlayerAuthority set in their NetworkIdentity.
      Previously only player objects could have local authority, which meant that the NetworkTransform (and other components) could only be used for controlling the single player object for a connection on a client. With these changes it is possible to have multiple objects on a client that are locally controlled.
     
    macagu and Lelon like this.
  6. Lork

    Lork

    Joined:
    Jul 12, 2013
    Posts:
    79
    They added client authority for non player objects, but NetworkTransform still doesn't work with such objects unless they are owned by the host.
     
  7. emulo2

    emulo2

    Joined:
    Apr 15, 2015
    Posts:
    21
    I have the same problem. Do you have a solution?
     
  8. sbob322

    sbob322

    Joined:
    May 30, 2014
    Posts:
    12
    I am encountering a similar problem.

    I have an object pool so the first player/server in makes a bunch of the objects needed for the game. When a player needs an object from the pool they tell the server and get it and place it where they need it. Everything works to this point. Each game object has a NetworkIdentity and a NetworkTransform. The host can move game objects and everything works properly. The joining client can see the host moving and doing stuff but the client cannot do anything themselves and even moving stuff in the editor while playing doesn't show up on the host and the objects pop back to where they are on the server. I have been trying to call RemoveClientAuthority, setting LocalPlayerAuthority, and AssignClientAuthority on the object for the player to control and nothing seems to work.
     
  9. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    Hi All,
    I also encounted same problem and able to sovle this issue (with some error) using this forum discussion(worth to read).
    Forum:
    UNET Would be nice to be able to "switch" authority from one client to another

    I am able to add or remove authority using this code. The function will run, as trigger hit with the required object:
    Code (CSharp):
    1. public void AssignObjectAuthority(string objectName)
    2.     {
    3.         if (!isLocalPlayer)
    4.         {
    5.             return;
    6.         }
    7.  
    8.         Debug.Log("IDs: " + this.GetComponentInChildren<NetworkIdentity>().connectionToServer.connectionId.ToString());
    9.         CmdAssignObjectAuthorityToClient(objectName);
    10.  
    11.     }
    12.  
    13.     public void RemoveObjectAuthority(string objectName)
    14.     {
    15.         if (!isLocalPlayer)
    16.         {
    17.             return;
    18.         }
    19.  
    20.         Debug.Log("IDs: " + this.GetComponentInChildren<NetworkIdentity>().connectionToServer.connectionId.ToString());
    21.         CmdRemoveObjectAuthorityToClient(objectName);
    22.  
    23.     }
    24.  
    25.     [Command]
    26.     public void CmdAssignObjectAuthorityToClient(string objectName)
    27.     {
    28.  
    29.         Debug.Log("Object whose authority going to assign :" + objectName);
    30.         GameObject ObjectAuthorityRequired = GameObject.Find(objectName);
    31.         ObjectAuthorityRequired.GetComponentInChildren<NetworkIdentity>().AssignClientAuthority(this.GetComponentInChildren<NetworkIdentity>().connectionToClient);
    32.     }
    33.  
    34.     [Command]
    35.     public void CmdRemoveObjectAuthorityToClient(string objectName)
    36.     {
    37.  
    38.         Debug.Log("Object whose authority going to remove :" + objectName);
    39.         GameObject ObjectAuthorityRequired = GameObject.Find(objectName);
    40.         ObjectAuthorityRequired.GetComponentInChildren<NetworkIdentity>().RemoveClientAuthority(this.GetComponentInChildren<NetworkIdentity>().connectionToClient);
    41.     }
    It is working(seems to me) but with this error(sometime):

    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.Networking.NetworkTransform.SendTransform () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkTransform.cs:1103)
    UnityEngine.Networking.NetworkTransform.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkTransform.cs:1031)