- Home /
Networking Unity can't get players on network to change each others variables.
HI all,
recently started networking this week and i'm trying get to get two players prefabs that have been instantiated on the network to communicate but I'm unsure how to do that... I've heard of Serialize and RPC but not sure how to implement it with what I have..
Basically, player1(client) cannot move as the server hasn't updated the client on plaer2s position if player2(server) moves away as the collisionexit wont register over the network, along with any attacks that will effect his health int to kill him. The variables seem unaffected when done over the network..the networkview only picks up the transformation when I want it to pick up the transformation, and access to variables in two of the scripts attached to the opposing player..
Networkmanager.cs
public void StartServer() { Network.InitializeServer (15, 25001, !Network.HavePublicAddress()); MasterServer.RegisterHost (typeName, gameName); } void OnServerInitialized() { Debug.Log("Server Initialized"); //network1.playerBalanceMech (); SpawnPlayer(); } // 2nd part private void RefreshHostList() // refresh active Server Hosting Lists, to join the server { MasterServer.RequestHostList (typeName); } void OnMasterServerEvent(MasterServerEvent msEvent) // { if (msEvent == MasterServerEvent.HostListReceived) hostList = MasterServer.PollHostList (); } private void JoinServer(HostData hostData) { Network.Connect (hostData); } void OnConnectedToServer() { Debug.Log ("Server Joined"); SpawnPlayer2(); } // Game Commands [RPC] public void SpawnPlayer() { // Player 1 Network.Instantiate (player1prefab, pos1.player1Start, Quaternion.identity, 0); } [RPC] public void SpawnPlayer2() { Network.Instantiate (player2prefab, pos2.player2Start, Quaternion.identity, 0); }
Offline, I can access the variables and have the game completed.. however I've never had much experience with this sort of thing.
Example on how I tried to alter p2's variables when p1 collides with him
Player1.cs
// Network stuffs
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if (stream.isWriting)
{
stream.Serialize(ref moveAfterCollision2.KeyEnabled_N);
}
else
{
stream.Serialize(ref moveAfterCollision2.KeyEnabled_N);
}
}
public void OnTriggerExit2D(Collider2D coll)
{
if (coll.gameObject.tag == "Player2")
{
print ("Player 1 left Player 2");
KeyEnabled_M = true;
moveAfterCollision2.KeyEnabled_N = true;
}
}
This is really hard to explain, but I hope somebody can help! :)
Craig