2

I am trying to spawn 2 Players (host and client) at 2 different locations.

I have no idea how to do this because the player are automatically spawned by the network manager.

I have tried the following but failed horribly :(.

[Command]
void CmdSpawn()
{
    var go = (GameObject)Instantiate(
              gameObject, 
              transform.position + new Vector3(0,1,0), 
              Quaternion.identity);

    NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
}

How Can I Spawn Player object at a specific location?

Rana
  • 1,675
  • 3
  • 25
  • 51

4 Answers4

7

NetworkManager already supports spawn positions.

Just add GameObjects where you want players to spawn and give them the NetworkStartPosition component. NetworkManager will automatically detect the start positions and automatically use them according to your setting of "Player Spawn Method" either "Random" (one spawn position is randomly chosen for every player) or "Round Robin" (player #0 spawns at first position, player #1 at second and so forth).

Thomas Hilbert
  • 3,559
  • 2
  • 13
  • 33
  • I did attach that component but what do I do after? how do I tell which player to spawn where? – Rana Mar 26 '16 at 22:14
  • I already did explain. Which part of it is unclear to you? Of course if you already have implemented custom spawning logic to solve the start position problem you should remove that, so you don't interfere with NetworkManager's implementation. – Thomas Hilbert Mar 26 '16 at 22:46
  • Is there a possibility to know/tell Unity in which order the NetworkStartPositions shall be used? – derHugo Jan 23 '18 at 23:38
2

Is there any problems with changing position in a script for player object? For example you could try adding following script on your player object:

void Start () {
    if (isServer) // host runs
    {
        transform.position = new Vector3(0,0,0);
    }
    else if (isClient) // client runs
    {
        transform.position = new Vector3(10,0,10);
    }
}
LapisLazuli
  • 406
  • 4
  • 8
0

i was facing a similar problem, here is the solution in this thread, read til the end.

http://forum.unity3d.com/threads/which-function-to-override.391076/

MJC
  • 45
  • 2
  • 6
0

Make a GameObject and attach a NetworkStartPosition to it. Then, place it somewhere. If you want more than 1 spawn point, you can CTRL+D that object. Then, go to NetworkManager and select Round Robin for spawning first to first, second to second etc. select Random for random spawn points.

kahveciderin
  • 312
  • 1
  • 3
  • 22