- Home /
How to Connect Difficult Object using UnityEngine.Network
My idea is to make multiplayer game based on physics and collisions. All the movings of characters are created by movings and rotation of Main body. I mean I have Sphere as main body, Shoulders as child with fixedRigidbody, hands as childs connected with Character Joint to the shoulders. Sword (on the same level as body) Connected with Characted Joint to the hand, Camera(on the same level as body) with the script To follow the body. And all that stuff is under Empty Player Object.
That should be the Player prefab for Network, however i met difficulties with synchronizing of all the parts of the player on the server, because only parent GameObject might have NetworkIdentity and NetworkTransform.
Thus i decided to instantiate parts of the body while the playig on server. EmptyPlayer Prefab contains script with method [Command] void CmdPlayerSpawn() that contatins links on the prefabs connected in inspector and their NetworkServer.SpawnWithClientAuthority. Moreover links of Joints were also added(Sword to hand) (Camera to follow body)
[Command]
void CmdPlayerSpawn()
{
_body = (GameObject)Instantiate(body, transform.position, Quaternion.identity);
NetworkServer.SpawnWithClientAuthority(_body, connectionToClient);
_sword = (GameObject)Instantiate(sword, _body.transform.Find("LeftGlove").position + Vector3.up, Quaternion.Euler(0,0,0));
NetworkServer.SpawnWithClientAuthority(_sword, connectionToClient);
_sword.GetComponent<CharacterJoint>().connectedBody = _body.transform.Find("LeftGlove").GetComponent<Rigidbody>();
_camera = (GameObject)Instantiate(camera, transform.position+ Vector3.up*8, Quaternion.Euler(40,0,0));
NetworkServer.SpawnWithClientAuthority(_camera, connectionToClient);
_camera.GetComponent<CameraBehaviour>().objectToFollow = _body;
Cameralink = _camera.GetComponent<Camera>();
}
AAAND. I am trying to play. While playing as host everything goes ok. Player is quite... playable. BUT As soon as i connect from other simulation as Client the client game crashes(throws NullReferenceExeptions) and Spawns 2 PlayerObjects (LMAO) ((that is seen on the host player account lol). I really cant understand how to execute my idea.
May be you could help me?