First time trying networking in Unity. Love how easy they make it but still having bugs that bewilder me. Here’s the current situation.
After finding the server to connect to on the masterserver, the client calls this…
private void JoinServer(HostData hostData)
{
Network.Connect (hostData);
GameObject.Find ("player_type").tag = "slave";
Application.LoadLevel ("space!");
}
And the host calls this…
void OnPlayerConnected(NetworkPlayer player)
{
Application.LoadLevel ("space!");
}
They both load the level, so I assume they’re connected.
The level consists of three objects, an obstacle and two units. Each of the units is particular to each player, but otherwise is indistinguishable from the other. They both have a Network View component, Reliable Delta Compressed, and attached to a “update_pos” script on each of the objects respectively. The update_pos script looks like this…
private int packet_num = 0;
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if (stream.isWriting) {
Debug.Log ("Sending data: " + packet_num++);
Vector3 pos = new Vector3();
Quaternion rot = new Quaternion();
pos = this.transform.position;
rot = this.transform.rotation;
stream.Serialize(ref pos);
stream.Serialize(ref rot);
} else if (stream.isReading) {
Vector3 pos = new Vector3();
Quaternion rot = new Quaternion();
stream.Serialize (ref pos);
this.transform.position = pos;
stream.Serialize (ref rot);
this.transform.rotation = rot;
}
}
Basically when the server moves their unit, the unit moves around the map on both the client and the server. However, when the client moves their unit, it only moves around on the client. “Sending data” appears in the Console when the server moves, but not when the client moves. I even tried giving the client control of the server’s unit, and still when the server moved it, it moved, when the client moved it, it didn’t. It would even jump to wherever the server moved it to when the server moved it (as expected). I’m stumped. Any help? I told my team I would figure out basic networking by Friday, and this is the only thing stopping me from being there.