Third Person Camera in a multiplayer game

I am trying to make a game like ‘What the box’ which is a multiplayer game. To make use of third person camera, I added a camera as child of player which continuously follows it. This worked fine for a single host but as soon as a client connected the camera focused on the client; even if the host was controlled, its actions were captured by the clients cam. What is the problem here or does anyone have any better solution?

3331212–259666–CameraMovement.cs (774 Bytes)
3331212–259667–PlayerMovement.cs (1.35 KB)

Sorry, not downloading your source files. Generally you should not make cameras part of your player prefab, since you’ll end up with as many cameras in the scene on every client as there are players. Create one camera, and if you want it a child of the player then set it as a child of the player after the player is spawned on that client. Check for isLocalPlayer to see if that is the right player to child your camera to.

On your player you could include code something like this on a NetworkBehavior:

void Start()
{
    if (isLocalPlayer)
    {
        GameObject cameraObject = GameObject.FindGameObjectWithTag("MainCamera");
        if (cameraObject != null)
        {
            cameraObject.transform.parent = gameObject.transform;
        }
        else
        {
            Debug.Log("Couldn't find MainCamera in scene");
        }
    }
}

Try enabling the camera movement script only on the localplayer
You can try either this in your cameramovement script:

void LateUpdate () {
        if (!isLocalPlayer) {
            return;
        }
        mousex += Input.GetAxis ("Mouse X")*mousesensitvity;
        mousey -= Input.GetAxis ("Mouse Y")*mousesensitvity;
        //mousey = Mathf.Clamp (mousey,yminmax.x,yminmax.y);

        Vector3 rotatecam = new Vector3 (mousey,mousex);
        transform.eulerAngles = rotatecam;

        transform.position = player.position -transform.forward*distancefromtarget;
    }

Or just by disabling the script by default and have it enabled in a OnStartLocalPlayer function in your playermovement script.

I hope I helped you out on this!