Search Unity

Help: Trying to spawn player in location based on user input

Discussion in 'Multiplayer' started by Zefalcon, Sep 7, 2017.

  1. Zefalcon

    Zefalcon

    Joined:
    Jun 17, 2016
    Posts:
    7
    Hello. I'm working on a multiplayer application for a classroom. Since the seats are set up in tables of six, we had the idea of spawning players in different locations based on where they were sitting in the class. To do this, I have the user selecting what table number and seat number they are at from a dropdown menu before connecting to the server. They would then spawn at an offset based on their table and seat.

    The problem I'm running into is that, while the player acting as host spawns in the correct location chosen, the clients instead spawn at whatever location the host did. I assume this has something to do with the input not getting passed correctly from the clients, but I'm unsure of how to go about fixing it.

    I am using a custom NetworkManager to control the spawn locations, which is shown below.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class TableSpawnNetworkManager : NetworkManager {
    7.  
    8.     public GameObject TableSelector; //TableSelector should have 6 buttons and a text box.  Buttons determine which table player is at, text box determines what seat they are in.
    9.  
    10.     int tableSelected = 0;
    11.     int seatSelected = 0;
    12.  
    13.     Vector3[] tableLocations;
    14.     Vector3[] seatOffsets;
    15.  
    16.     /// <summary>
    17.     /// Tables are a hexagon shape (I believe pointed end towards the front of the room; if not, will revise).
    18.     /// Seats, then, should be placed on the corners of the opposite hexagon (flat towards the front of the room).
    19.     /// This results in 2 seats on the "upper" plane, 2 on the "equatorial" plane, and 2 on the "lower" plane.
    20.     /// The upper and lower seats will have offsets of 1/2 a side in the x direction and sqrt(3)/2 of a side in the y direction.
    21.     /// The equatorial seats, then, will have offsets only in the x direction of a full side length.
    22.     /// This creates an easy method of finding world-space seat locations based off of the table location in-world.
    23.     /// </summary>
    24.     static float UP_OFFSET = 0.866f * 5; //Hexagonal offset in the vertical(y) direction.  For seats 1/2(positive), 4/5(negative).
    25.     static float HALF_SIDE_OFFSET = 0.5f * 5; //Hexagonal offset in the horizontal direction for upper and lower sides.  For seats 2/4(positive), 1/5(negative).
    26.     static float FULL_SIDE_OFFSET = 1f * 5; //Hexagonal offset in the horizontal direction for equatorial sides.  For seats 3(positive), 6(negative).
    27.  
    28.     /// <summary>
    29.     /// Tables placed 2 units apart (1 unit horizontally from 0,0,0 each, and a max of 2 units vertically)
    30.     /// </summary>
    31.     static float VERTICAL_TABLE_OFFSET = 400f;
    32.     static float HORIZONTAL_TABLE_OFFSET = 200f;
    33.  
    34.     private void Awake() {
    35.         seatOffsets = new Vector3[7]; //7 both for ease of access and in case of extra students/instructors
    36.         seatOffsets[0] = Vector3.zero;
    37.         seatOffsets[1] = new Vector3(-HALF_SIDE_OFFSET, UP_OFFSET, 0);
    38.         seatOffsets[2] = new Vector3(HALF_SIDE_OFFSET, UP_OFFSET, 0);
    39.         seatOffsets[3] = new Vector3(FULL_SIDE_OFFSET, 0, 0);
    40.         seatOffsets[4] = new Vector3(HALF_SIDE_OFFSET, -UP_OFFSET, 0);
    41.         seatOffsets[5] = new Vector3(-HALF_SIDE_OFFSET, -UP_OFFSET, 0);
    42.         seatOffsets[6] = new Vector3(-FULL_SIDE_OFFSET, 0, 0);
    43.  
    44.         tableLocations = new Vector3[7]; //7 both for ease of access and for the case of instructors
    45.         tableLocations[0] = Vector3.zero;
    46.         tableLocations[1] = new Vector3(-HORIZONTAL_TABLE_OFFSET, VERTICAL_TABLE_OFFSET, 0);
    47.         tableLocations[2] = new Vector3(HORIZONTAL_TABLE_OFFSET, VERTICAL_TABLE_OFFSET, 0);
    48.         tableLocations[3] = new Vector3(-HORIZONTAL_TABLE_OFFSET, 0, 0);
    49.         tableLocations[4] = new Vector3(HORIZONTAL_TABLE_OFFSET, 0, 0);
    50.         tableLocations[5] = new Vector3(-HORIZONTAL_TABLE_OFFSET, -VERTICAL_TABLE_OFFSET, 0);
    51.         tableLocations[6] = new Vector3(-HORIZONTAL_TABLE_OFFSET, -VERTICAL_TABLE_OFFSET, 0);
    52.     }
    53.  
    54.     // Use this for initialization
    55.     void Start () {
    56.        
    57.     }
    58.    
    59.     // Update is called once per frame
    60.     void Update () {
    61.        
    62.     }
    63.  
    64.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
    65.         OnServerAddPlayer(conn, playerControllerId, null);
    66.     }
    67.  
    68.     public override void OnClientConnect(NetworkConnection conn) {
    69.         ClientScene.AddPlayer(conn, 0);
    70.     }
    71.  
    72.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader) {
    73.         Vector3 location = tableLocations[tableSelected] + seatOffsets[seatSelected];
    74.         Debug.Log(location.x + "," + location.y + "," + location.z);
    75.  
    76.         TableSelector.gameObject.SetActive(false);
    77.  
    78.         GameObject player = GameObject.Instantiate(playerPrefab, location, Quaternion.identity);
    79.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    80.  
    81.         //base.OnServerAddPlayer(conn, playerControllerId, extraMessageReader);
    82.     }
    83.  
    84.     public override void OnClientDisconnect(NetworkConnection conn) {
    85.         TableSelector.gameObject.SetActive(true);
    86.         base.OnClientDisconnect(conn);
    87.     }
    88.  
    89.     public void SetTableNum(int table) {
    90.         tableSelected = table;
    91.     }
    92.  
    93.     public void SetSeatNum(int seat) {
    94.         seatSelected = seat;
    95.     }
    96.    
    97. }
    98.  
    If someone could help me solve this problem, it would be very appreciated.
     
  2. Zefalcon

    Zefalcon

    Joined:
    Jun 17, 2016
    Posts:
    7
    Update: I circumvented the spawning problem by moving the player object after they spawn. Still haven't figured out a good way to do it beforehand.