- Home /
Network initializing player
Hello,
I'm trying to get co-op working with unity but i got a little problem while spawning new players.
With the first script i can't get the new player see the old player, i understand why it's not doing it. But when i do it the second way, I get every time someone joins and spawns, the olders also get a new character controller.
there is the first way (that is obvious for me why it doesn't work):
void Start()
{
Debug.Log("Spawning the player locally");
Spawnplayer();
}
void Spawnplayer()
{
Debug.Log("Spawing another player");
Network.Instantiate(PlayerPrefab, transform.position, transform.rotation, 0);
}
The other way:
void Start()
{
Debug.Log("Spawning the player locally");
//Spawnplayer();
networkView.RPC("Spawnplayer", RPCMode.AllBuffered);
}
[RPC]
void Spawnplayer()
{
Debug.Log("Spawing another player");
Network.Instantiate(PlayerPrefab, transform.position, transform.rotation, 0);
//Instantiate(PlayerPrefab, transform.position, transform.rotation);
}
I'm going on another direction, so i got the following:
void Start()
{
Debug.Log("Spawning the player locally");
networkView.RPC("PlayerLoadedLevel", RPC$$anonymous$$ode.AllBuffered, Network.player);
//Spawnplayer();
//networkView.RPC("Spawnplayer", RPC$$anonymous$$ode.AllBuffered);
}
[RPC]
void Spawnplayer(NetworkPlayer player, Transform[] playerTrans, NetworkPlayer[] players)
{
if(player == Network.player)
{
Debug.Log("Spawing another player");
GameObject playerObject = Network.Instantiate(PlayerPrefab, transform.position, transform.rotation, 0) as GameObject;
playerObject.name = "Player"+player.ToString();
for(int i=0; i<players.Length; i++)
{
GameObject pl = Instantiate(PlayerPrefab, playerTrans[i].transform.position, playerTrans[i].transform.rotation) as GameObject;
pl.name = "Player"+players[i].ToString();
}
//Instantiate(PlayerPrefab, transform.position, transform.rotation);
}
}
[RPC]
void PlayerLoadedLevel(NetworkPlayer LoadedPlayer)
{
if(Network.isServer)
{
Transform[] playerTrans = GameObject.FindGameObjectsWithTag("Player");
networkView.RPC("Spawnplayer", RPC$$anonymous$$ode.AllBuffered, LoadedPlayer, playerTrans, Network.connections);
}
}
But when i run this, i get the following error: Failed to invoke arriving RPC method because the parameters didn't match the function declaration. 'Spawnplayer' of 'NetworkSpawn'.
But i've no idea what i'm doing wrong.
Answer by Branx · Apr 02, 2012 at 01:32 PM
Script Reference says: Valid RPC parameters are int, float, string, NetworkPlayer, NetworkViewID, Vector3 and Quaternion. Having a Transform[] as parameter type is still legal because you could call the function the usual way. However the RPC call can't handle it - correctly logging an Error that directs you to the function declaration of SpawnPlayer.
hmmm, well i took a look at the m2h networking tutorial. I saw nothing really specially about the way how they did it. so how can spawn the player and let him also see the older, already spawned, players?
I don't know that Tutorial, but the most simple way is to call Network.Instantiate() and let Unity do the rest. Don't place Network.Instantiate() in a RPC method. Call it directly on the client, e.g. in Start();
but when i do that, the new player cant see the earlier spawned players. I've added the networkview on the player prefab and its observing the player Transform (https://lh3.googleusercontent.com/-2V2tZ_$$anonymous$$yVC0/T3nYw_nOGkI/AAAAAAAAAI0/r7fw3BZhk$$anonymous$$o/s720/$$anonymous$$nipsel.PNG)
now i read the documentation about the network.instantiate again, i see it's bufferd. The only problem is that the new instantiated player cant see the already instantiated players
Could it be your buffered calls somehew get lost? Did you check "Run in background" ? Does the same problem occur with normal buffered RPC calls. And are you sure your objects are not on top of each other because a Walker script or smth is acticated on both? Sorry if these are too obvious questions, but this looks like only some $$anonymous$$or detail is missing somewhere. Can you update the posted scripts to your current?
Your answer
Follow this Question
Related Questions
RPC function to update clients varialbe 0 Answers
Mutliplayer SetActive(true) 0 Answers
Network - Killing Player, Then Respawn - Issue with RPC 1 Answer
Disconnect a player from a network 1 Answer
Network.Destroy(), Couldn't perform remo 0 Answers