Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Dynamic Wall Creation Networked - Not showing walls spawned from client on the host.

Discussion in 'Editor & General Support' started by Vinnyd21, Nov 7, 2018.

  1. Vinnyd21

    Vinnyd21

    Joined:
    Sep 5, 2018
    Posts:
    5
    I'm having issues with networking a script to spawn dynamic walls. I can get it "working" in several different capacities, but none to the actual desired effect. In this state, both the client player and host player can spawn and draw the walls, but only the client is able to view the walls spawn by the host. The host has no knowledge of the walls spawned by the client.

    Here is the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. [RequireComponent(typeof(Camera))]
    7. public class DrawWalls_Network : NetworkBehaviour {
    8.  
    9.     public bool creating;
    10.     public GameObject startPrefab;
    11.     public GameObject endPrefab;
    12.     public GameObject[] wallPrefabs;
    13.     GameObject wall;
    14.     public int currentWall = 0;
    15.     Camera drawCam;
    16.     //public NHNetworkedPoolWalls[] walls;
    17.  
    18.     // Use this for initialization
    19.     void Start ()
    20.     {
    21.         drawCam = GetComponent<Camera>();
    22.         //if (NetworkServer.active)
    23.             //pools = FindObjectsOfType<NHNetworkedPoolWalls>();
    24.  
    25.         SelectWall();
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update ()
    30.     {
    31.         if (!isLocalPlayer)
    32.             return;
    33.  
    34.         GetInput();
    35.  
    36.         int previousWall = currentWall;
    37.  
    38.         if ((Input.GetAxis("Mouse ScrollWheel") > 0f))
    39.         {
    40.             if (currentWall >= wallPrefabs.Length - 1)
    41.                 currentWall = 0;
    42.             else
    43.                 currentWall++;
    44.         }
    45.         if ((Input.GetAxis("Mouse ScrollWheel") < 0f))
    46.         {
    47.             if (currentWall <= 0)
    48.                 currentWall = wallPrefabs.Length - 1;
    49.             else
    50.                 currentWall--;
    51.         }
    52.  
    53.         if (previousWall != currentWall)
    54.         {
    55.             SelectWall();
    56.         }
    57.     }
    58.  
    59.     public void GetInput()
    60.     {
    61.         if (Input.GetMouseButtonDown(0))
    62.         {
    63.             SetStart();
    64.         }
    65.         else if (Input.GetMouseButtonUp(0))
    66.         {
    67.             SetEnd();
    68.         }
    69.         else
    70.         {
    71.             if (creating)
    72.             {
    73.                 Adjust();
    74.             }
    75.         }
    76.     }
    77.  
    78.     public void SetStart()
    79.     {
    80.         creating = true;
    81.         startPrefab.transform.position = getWorldPoint();
    82.         wall = (GameObject)Instantiate(wallPrefabs[currentWall], startPrefab.transform.position, Quaternion.identity);
    83.         NetworkServer.Spawn(wall);
    84.         //CmdSpawnWall(currentWall);
    85.     }
    86.  
    87.     //[Command]
    88.     //public void CmdSpawnWall(int currentWall)
    89.     //{
    90.         //wall = (GameObject)Instantiate(wallPrefabs[currentWall], startPrefab.transform.position, Quaternion.identity);
    91.         //NetworkServer.Spawn(wall);
    92.     //}
    93.  
    94.     public void SetEnd()
    95.     {
    96.         creating = false;
    97.         endPrefab.transform.position = getWorldPoint();
    98.     }
    99.  
    100.  
    101.     public void Adjust()
    102.     {
    103.         endPrefab.transform.position = getWorldPoint();
    104.         AdjustWall();
    105.     }
    106.  
    107.     public void AdjustWall()
    108.     {
    109.         startPrefab.transform.LookAt(endPrefab.transform.position);
    110.         endPrefab.transform.LookAt(startPrefab.transform.position);
    111.         float distance = Vector3.Distance(startPrefab.transform.position, endPrefab.transform.position);
    112.         wall.transform.position = startPrefab.transform.position + distance / 2 * startPrefab.transform.forward;
    113.         wall.transform.rotation = startPrefab.transform.rotation;
    114.         wall.transform.localScale = new Vector3(wall.transform.localScale.x, wall.transform.localScale.y, distance);
    115.     }
    116.  
    117.     Vector3 getWorldPoint()
    118.     {
    119.         Ray ray = drawCam.ScreenPointToRay(Input.mousePosition);
    120.         RaycastHit hit;
    121.         if(Physics.Raycast(ray, out hit))
    122.         {
    123.             return hit.point;
    124.         }
    125.         return Vector3.zero;
    126.     }
    127.  
    128.     void SelectWall()
    129.     {
    130.         int i = 0;
    131.         foreach (GameObject wall in wallPrefabs)
    132.         {
    133.             if (i == currentWall)
    134.                 wall.gameObject.SetActive(true);
    135.             else
    136.                 wall.gameObject.SetActive(false);
    137.             i++;
    138.         }
    139.     }
    140. }
    The CmdSpawnWall, that is currently commented out, will make it so that the client won't spawn anything, and that the host only spawns the prefab post without changing the scale