Search Unity

How to get NetworkServer.Spawn object netId at Client?

Discussion in 'Multiplayer' started by jsc18, Aug 28, 2016.

  1. jsc18

    jsc18

    Joined:
    Aug 14, 2016
    Posts:
    5
    Code (CSharp):
    1. void Grenade()
    2.     {
    3.         if (!isLocalPlayer)
    4.             return;
    5.  
    6.         /*Vector3 position = new Vector3(armcamera.transform.FindChild("Grenade").transform.position.x,
    7.                                                armcamera.transform.FindChild("Grenade").transform.position.y,
    8.                                                armcamera.transform.FindChild("Grenade").transform.position.z);
    9.  
    10.         GameObject Grenade = (GameObject)Instantiate(transform.GetComponent<Weapon>().grenade, position, Quaternion.identity);
    11.  
    12.         gren.Add(i, Grenade);*/
    13.         CmdSpawn(name, i);
    14.         gren[i].GetComponent<Rigidbody>().AddForce(armcamera.transform.forward * forcespeed, ForceMode.Impulse);
    15.     }
    16.  
    Code (CSharp):
    1. [Command]
    2.     void CmdSpawn(string name, int i)
    3.     {
    4.         Vector3 position = new Vector3(armcamera.transform.FindChild("Grenade").transform.position.x,
    5.                                                armcamera.transform.FindChild("Grenade").transform.position.y,
    6.                                                armcamera.transform.FindChild("Grenade").transform.position.z);
    7.  
    8.         GameObject Grenade = (GameObject)Instantiate(transform.GetComponent<Weapon>().grenade, position, Quaternion.identity);
    9.  
    10.         gren.Add(i, Grenade);
    11.  
    12.         Player player = GameManager.GetPlayer(name);
    13.  
    14.         NetworkServer.SpawnWithClientAuthority(Grenade, base.connectionToClient);
    15.                                                         // player.gameObject
    16.         /*if(!isserver)
    17.             GetComponent<Grenade>().SendToClient(name, Grenade.GetComponent<NetworkIdentity>().netId, i);*/
    18.         /*if(GameManager.GetPlayer(name).GetComponent<NetworkIdentity>().isServer)
    19.             if(!isLocalPlayer)*/
    20.         //RpcSpawn(name, i, Grenade.GetComponent<NetworkIdentity>().netId);
    21.     }
    22.  
    23.     [ClientRpc]
    24.     void RpcSpawn(string name, int i, NetworkInstanceId netid)
    25.     {
    26.         if (!isServer)
    27.         {
    28.             Anim anim = GameManager.GetPlayer(name).GetComponent<Anim>();
    29.  
    30.             GameObject Grenade = ClientScene.FindLocalObject(netid);
    31.  
    32.             anim.gren.Add(i, Grenade);
    33.         }
    34.     }
    Grenade function is added to the animation event in the grenade throwing animation.

    gren is dictionary. (public Dictionary<int, GameObject> gren = new Dictionary<int, GameObject>())

    I want to synchronize with the position, movement(Addforce), spawn of the grenade between the server(client) and the client.

    CmdSpawn function spawns a grenade with the authority of the client using the NetworkServer.Spawn With Client Authority (Grenade, base.connectionToClient).

    Each client can throw grenade only added to the gren dictionary. Otherwise the Exception is occured.(KeyNotFoundException: The given key was not present in the dictionary.) because not added to grenade to gren with the key.

    Because grenade is spawned by the server, There is no way to know the grenade netid on a client throwing grenade.

    because CmdSpawn [Command] function is invoked on the server, Throwing grenades and explosions are not a problem on the server. but only issue is occured on a client. (from client to client and client to server)

    Of course, I know ClientScene.FindLocalObject(NetworkInstanceId netId) and can find local object on a client. but no way to know grenade netid spawned by server on a client throwing grenade.

    As soon as spawn a grenade on the server, how can I add grenade spawned by server to gren on a client? or

    How can I get grenade netid spawned by server on a client? else Is there no other way to solve this problem?

    i'm using translator and Please understand that I'm not good at english.
     
    Last edited: Aug 29, 2016