VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
virtual void AVR.Net.AVR_PlayerSpawn.spawnServerRpc ( ulong  clientId)
inlineprotectedvirtual

Definition at line 41 of file AVR_PlayerSpawn.cs.

42  {
43  AVR_DevConsole.cprint("Client #"+clientId+" requested spawn", this);
44 
45  if(NetworkManager.ConnectedClientsList.Count + 1 > max_players) {
46  AVR_DevConsole.cerror("Connected client would exceed the maximum number of allowed players ("+max_players+"). Disconnecting client.", this);
47  NetworkManager.DisconnectClient(clientId);
48  return;
49  }
50 
51  Transform spawnLocation = transform;
52  if(spawnLocations.Count>0) {
53  spawnLocation = spawnLocations[(int)clientId % spawnLocations.Count];
54  }
55 
56  try {
57  // If the client already has a playerobject, destroy it before creating a new one.
58  if (NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(clientId) != null)
59  {
60  AVR_DevConsole.cprint("Client #"+clientId+" already has a playerNetworkObject! Despawning.", this);
61  NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(clientId).Despawn(destroy: true);
62  }
63 
64  // Instantiate and spawn the object
65  AVR_DevConsole.cprint("Instantiating playerPrefab as playerObject for client #"+clientId, this);
66  GameObject obj = Instantiate(getPrefab().gameObject, spawnLocation.position, spawnLocation.rotation);
67  obj.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
68  }
69  // Deal with errors
70  catch(System.Exception e) {
71  AVR_DevConsole.cerror("PlayerSpawn failed with following exception: "+e.Message, this);
73  AVR_DevConsole.cerror("Disconnecting client due to failed spawn.", this);
74  NetworkManager.DisconnectClient(clientId);
75  }
76  }
77  }
List< Transform > spawnLocations
virtual NetworkObject getPrefab()