Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[UNet] Spawned GameObject's layer not syncing over network

Discussion in 'UNet' started by slimabob, Jan 7, 2016.

  1. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    I've been wrestling with this for a couple of days now. Basically what this is supposed to do is once the player presses "B", spawn a "ghost brick" which is essentially a representation of the actual brick that follows where the player is looking. If the player clicks, it will create an actual brick at the location of the ghost brick. This works perfectly fine on the server, however on the client, the ray seems to interact with the ghost brick, even though the Layer Mask is supposedly set up to ignore the layer it is on (Ghost (or layer 10)). Here's a video to better explain what's going on:



    And here's my code, I know that it isn't efficient in lots of places, but I'd really just like to get it working before I worry about efficiency. (also sorry for it being messy at several points, I started to lose interest in being neat a few hours into debugging). If there's anything that doesn't make sense here, please ask me to clarify and I will be more than happy to oblige.

    I'm seriously at my wit's end right now, I've tried pretty much everything I could think of to get this working, but to no avail.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class Building_MP : NetworkBehaviour {
    6.  
    7.     [SerializeField]
    8.     GameObject GhostGO;
    9.     public LayerMask LM;
    10.     [SerializeField]
    11.     private Transform camTransform;
    12.  
    13.     bool snap = false;
    14.     bool active = false;
    15.  
    16.     int ghostID;
    17.  
    18.     // Use this for initialization
    19.     public override void OnStartLocalPlayer () {
    20.         GetComponentInChildren<Camera>().transform.name = Random.Range(1, 100).ToString();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         RaycastHit hit;
    27.         Ray ray = this.gameObject.GetComponentInChildren<Camera>().ScreenPointToRay(Input.mousePosition);
    28.  
    29.         if (Input.GetKeyDown(KeyCode.B))
    30.         {
    31.             active = !active;
    32.             if(active)
    33.             {
    34.              
    35.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity, LM))
    36.                 {
    37.                     CmdSpawnGhostBrick(new Vector3(RoundToHalf(hit.point.x), RoundToHalf(hit.point.y), RoundToHalf(hit.point.z)));
    38.                 }
    39.                  
    40.             }
    41.             else
    42.             {
    43.                 CmdDestroyGhost();
    44.             }
    45.         }
    46.  
    47.         if (active && isLocalPlayer)
    48.         {
    49.             if (Input.GetKeyDown(KeyCode.R))
    50.             {
    51.                 snap = !snap;
    52.             }
    53.  
    54.             if (Input.GetButtonDown("Fire2"))
    55.             {
    56.  
    57.             }
    58.  
    59.             RaycastHit hit2;
    60.             if(Physics.Raycast(ray, out hit2, Mathf.Infinity, LM))
    61.             {
    62.                 //CmdDoTheDebug(hit2.transform.name);
    63.                 //Debug.Log(hit2.collider.tag);
    64.                 //CmdDoTheDebug(hit.transform.gameObject.layer.ToString());
    65.  
    66.                 Vector3 moveTransform;
    67.  
    68.                 if(!snap)
    69.                 {
    70.                     moveTransform = new Vector3(RoundToHalf(hit2.point.x), RoundToHalf(hit2.point.y) + GhostGO.GetComponent<Renderer>().bounds.size.y / 2, RoundToHalf(hit2.point.z)); //+ ghostBrick.GetComponent<Renderer>().bounds.size.y / 2
    71.                 }
    72.                 else
    73.                 {
    74.                     moveTransform = new Vector3(hit2.transform.position.x, hit2.transform.position.y + GhostGO.GetComponent<Renderer>().bounds.size.y, hit2.transform.position.z); //+ ghostBrick.GetComponent<Renderer>().bounds.size.y
    75.                 }
    76.  
    77.                 CmdMoveGhostBrick(moveTransform);
    78.  
    79.                 if (Input.GetButtonDown("Fire1"))
    80.                 {
    81.                     CmdPlaceBrick();
    82.                 }
    83.  
    84.                 if(Input.GetKeyDown(KeyCode.E))
    85.                 {
    86.                     CmdRotateBrick();
    87.                 }
    88.             }      
    89.         }
    90.     }
    91.  
    92.     [Command]
    93.     void CmdSpawnGhostBrick(Vector3 pos)
    94.     {
    95.         ghostID = Random.Range(0, 10000);
    96.         GameObject ghostBrick = Instantiate(GhostGO, pos, Quaternion.identity) as GameObject;
    97.         ghostBrick.name = "GhostBrick (" + ghostID + ")";
    98.         ghostBrick.tag = "Ghost";
    99.         ghostBrick.layer = LayerMask.NameToLayer("Ghost");
    100.         NetworkServer.Spawn(ghostBrick);
    101.         //Debug.Log("Hit at " + hit.point.ToString());
    102.         //Debug.Log("BrickPos " + ghostBrick.transform.position);
    103.     }
    104.  
    105.     [Command]
    106.     void CmdDoTheDebug(string name)
    107.     {
    108.         Debug.Log("Hit " + name);
    109.     }
    110.  
    111.     [Command]
    112.     void CmdPlaceBrick()
    113.     {
    114.         GameObject ghostBrick = GameObject.Find("GhostBrick (" + ghostID + ")");
    115.         GameObject newBrick = Instantiate(GhostGO, ghostBrick.transform.position, ghostBrick.transform.rotation) as GameObject;
    116.         NetworkServer.Spawn(newBrick);
    117.         newBrick.transform.name = GhostGO.transform.name;
    118.         newBrick.layer = 9;
    119.         newBrick.tag = "Brick";
    120.     }
    121.  
    122.     [Command]
    123.     void CmdRotateBrick()
    124.     {
    125.         GameObject ghostBrick = GameObject.Find("GhostBrick (" + ghostID + ")");
    126.         ghostBrick.transform.Rotate(Vector3.up * 90);
    127.     }
    128.  
    129.     //[Command]
    130.     //void CmdMoveGhostBrick(Vector3 newTrans)
    131.     //{
    132.     //    GameObject ghostBrick = GameObject.Find("GhostBrick (" + ghostID + ")");
    133.         //ghostBrick.transform.position = newTrans;
    134.     //    ghostBrick.transform.position = Vector3.MoveTowards(ghostBrick.transform.position, newTrans, Time.deltaTime * 1000f);
    135.         //}
    136.     //}
    137.  
    138.     [Command]
    139.     void CmdMoveGhostBrick(Vector3 newPos)
    140.     {
    141.         GameObject ghostBrick = GameObject.Find("GhostBrick (" + ghostID + ")");
    142.         //Debug.Log("Moving from: " + ghostBrick.transform.position + " to: " + newPos + ". Tag on ghostBrick is: " + ghostBrick.tag + " and it is on layer: " + ghostBrick.layer + ".");
    143.         ghostBrick.transform.position = newPos;
    144.      
    145.  
    146.     }
    147.  
    148.     [Command]
    149.     void CmdDestroyGhost()
    150.     {
    151.         GameObject ghostBrick = GameObject.Find("GhostBrick (" + ghostID + ")");
    152.         if(ghostBrick != null)
    153.         {
    154.             NetworkServer.Destroy(ghostBrick);
    155.         }
    156.     }
    157.  
    158.     public static float RoundToHalf(float f)
    159.     {
    160.         return f = Mathf.Round(f * 4f) * 0.25f;
    161.     }
    162. }
    163.  
    EDIT: Here's my Layers list just in case you need to see it:
     
  2. slimabob

    slimabob

    Joined:
    Jul 7, 2013
    Posts:
    23
    SOLVED: I was forgetting to include a ClientRPC, so the changes were not being applied to the client, only the server. Here's what I added just in case anybody runs into a similar issue down the road:

    Code (CSharp):
    1.     [Command]
    2.     void CmdSpawnGhostBrick(Vector3 pos)
    3.     {
    4.         ...
    5.  
    6.         RpcChangeGhostLayer(ghostBrick);
    7.     }
    8.  
    9.     [ClientRpc]
    10.     void RpcChangeGhostLayer(GameObject ghost)
    11.     {
    12.         ghost.name = "GhostBrick (" + ghostID + ")";
    13.         ghost.layer = 10;
    14.         ghost.tag = "Brick";
    15.     }
     
    ikillzombies and VrTechEx like this.
  3. VrTechEx

    VrTechEx

    Joined:
    Aug 4, 2013
    Posts:
    40
    That helped a lot Thanks !
     
    slimabob likes this.