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

VR Multiplayer - how to change gameobjects on bullet hit.

Discussion in 'Multiplayer' started by Jonasnik, Nov 18, 2016.

  1. Jonasnik

    Jonasnik

    Joined:
    Nov 18, 2016
    Posts:
    5
    Hello all.

    So i am working on this multiplayer VR game.
    Its like a paintball shooter, so what i want to do is to change colors on the scene gameobjects when hit by a bullet and then be sure that all players get the changes.

    Question 1:
    Do i need to spawn the gameobjects on server start to be able to change them across all clients?

    Question 2:
    How would you handle the command to get all clients to see the change?

    What i have tried so far:
    Code (CSharp):
    1.  
    2. void OnCollisionEnter(Collision collision)
    3.     {
    4.         Debug.Log("cube hit");
    5.         if (collision.gameObject.tag == "Bullet")
    6.         {
    7.             objectColor = collision.gameObject.GetComponent<Renderer>().material.color;
    8.             objectID = this.gameObject;
    9.             CmdPaint(objectID, objectColor);
    10.         }
    11.     }
    12.  
    13.    
    14.  
    15.     [ClientRpc]
    16.     void RpcPaint(GameObject obj, Color col)
    17.     {
    18.         obj.GetComponent<Renderer>().material.color = col;      // this is the line that actually makes the change in color happen
    19.     }
    20.  
    21.  
    22.  
    23.  
    24.     [Command]
    25.     void CmdPaint(GameObject obj, Color col)
    26.     {
    27.         objNetId = obj.GetComponent<NetworkIdentity>();        // get the object's network ID
    28.         objNetId.AssignClientAuthority(connectionToClient);    // assign authority to the player who is changing the color
    29.         RpcPaint(obj, col);                                    // usse a Client RPC function to "paint" the object on all clients
    30.         objNetId.RemoveClientAuthority(connectionToClient);    // remove the authority from the player who changed the color
    31.     }

    Here i get the error that connectionToClient is null.

    Hope this is enough for you guys to give me some help, else let me know and i will try to elaborate more.

    Thanks!
     
  2. Jonasnik

    Jonasnik

    Joined:
    Nov 18, 2016
    Posts:
    5
  3. benzsuankularb

    benzsuankularb

    Joined:
    Apr 10, 2013
    Posts:
    132
    What is this object is it a player or just an object
     
  4. Jonasnik

    Jonasnik

    Joined:
    Nov 18, 2016
    Posts:
    5
    The object is a cube in the scene. The object is not spawned through the network manager, but is part of the scene already.
     
  5. benzsuankularb

    benzsuankularb

    Joined:
    Apr 10, 2013
    Posts:
    132
    if you create a server authority object (not player authority) you can't send command to server.

    Try this

    let think that OnCollisionEnter will be invoked on both client and server

    Code (CSharp):
    1.  
    2.     // Invoke on both server and client (if you done correct)
    3.     void OnCollisionEnter(Collision collision)
    4.     {
    5.         if (collision.gameObject.tag == "Bullet")
    6.         {
    7.             if (isServer)
    8.             {
    9.                 objectColor = collision.gameObject.GetComponent<Renderer>().material.color;
    10.                 obj.GetComponent<Renderer>().material.color = objectColor;
    11.                 RpcPaint(objectID, objectColor);
    12.             }
    13.         }
    14.     }
    15.  
    16.     [ClientRpc]
    17.     void RpcPaint(GameObject obj, Color col)
    18.     {
    19.         obj.GetComponent<Renderer>().material.color = col;      // this is the line that actually makes the change in color happen
    20.     }
     
    Last edited: Nov 25, 2016
    Jonasnik likes this.
  6. Jonasnik

    Jonasnik

    Joined:
    Nov 18, 2016
    Posts:
    5
    It worked somehow, will play around with it for a bit and see if it fits my needs. Thanks man! :)