Can't get character camera to work with Netcode

I am building a third person shooter with the ‘new’ Input System and Cinemachine. I have the basic setup of the things (network manager that points to my player prefab, is in the network objects lists, the player prefab has the network object attached to it and also the Client Network Transform, etc.). My prefab uses the Character Controller component and the movement works fine, also I can move my player in the direction of the camera which is what I want, but when it comes to any type of rotation things get messy, and are just not working.
My current setup is like this: the player prefab has a virtual camera as a child which points to the player prefab’s transform for the Follow and LookAt properties, and I have a main camera in the scene with a cinemachine brain component. At first I wasn’t sure that I was doing the ownership right (or maybe I am not still) since there is only one camera in the scene, but spawning multiple clients, they all move and look independently from each other, and also are moving in the camera’s direction of their own…

My controller looks like this (the working one without rotations)

public override void OnNetworkSpawn()
    {
        CinemachineVirtualCamera virtualCamera = GetComponentInChildren<CinemachineVirtualCamera>();

        if (IsOwner)
        {
            virtualCamera.Priority = 1;
        }
        else
        {
            virtualCamera.Priority = 0;
        }
    }

    private void Start()
    {
        characterController = GetComponent<CharacterController>();

        if (IsClient && IsOwner)
        {
            cameraTransform = Camera.main.transform;
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }

    void Update()
    {
        if (!IsLocalPlayer) return;

        Vector3 playerMoveDirection = new Vector3(inputMoveDirection.x, 0, inputMoveDirection.y);

        playerMoveDirection = playerMoveDirection.x * cameraTransform.right.normalized + playerMoveDirection.z * cameraTransform.forward.normalized;
        playerMoveDirection.y = 0;

        characterController.Move(playerSpeed * Time.deltaTime * playerMoveDirection);
    }

    public void OnMove(InputAction.CallbackContext context)
    {
        if (IsLocalPlayer)
        {
            inputMoveDirection = context.ReadValue<Vector2>();
        }
    }

As far as rotating towards camera, so far I tried the following (all in the update() alongside the movement):

Quaternion targetRotation = Quaternion.Euler(0, cameraTransform.eulerAngles.y, 0);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, playerRotationSpeed * Time.deltaTime);

This one worked for me perfectly in the single player stage of the game…

I also tried:

Quaternion targetRotation = Quaternion.LookRotation(playerMoveDirection, Vector3.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, playerRotationSpeed * Time.deltaTime);

And

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(playerMoveDirection), 0.15f);

But no luck, most of the time the camera was rotating constantly, without even touching the mouse, and when i did it was like i added a lot of force to the rotation and just kept going, the player was rotating indeed but in a horrendous manner, at times wherever i looked the player was rotating as a whole too (can’t remember which code created this behavior), looking down made the player rotate with his face downwards to the ground, either this or like i said, constant rotation from the camera.

this is the main camera in the scene (it has the maincamera tag, if it’s relevant or not)

and for the properties of the virtual camera, i am not using the 3rd person follow for the body, for some reason that way the camera just does not rotate around the player, it follows it but looking around it’s like looking with your phone camera at the scene, does not revolve around the player, framing transposer for body + pov for aim part worked for me… so maybe this can have something to do with it

Thank you for the effort of reading my problem and any tip or help is much appreciated!

this my player prefab structure

and the virtual camera’s properties

and also the (hopefully relevant) player prefab’s properties (the main empty object on top)

The issue lied in the fact that the virtual camera was a child of my player prefab, therefore every time I modified the player’s transform.rotation the camera was affected creating all sorts of weird behaviors.
Now my setup is like this: a main camera in the scene with a cinemachine camera without setting the lookAt and follow properties, handling this in the on network spawn function like this alongside ownership:

public override void OnNetworkSpawn()
    {
        if (IsOwner)
        {
            cameraTransform = GameObject.FindGameObjectWithTag("MainCamera").transform;
            virtualCamera = cameraTransform.GetComponent<CinemachineBrain>().ActiveVirtualCamera.VirtualCameraGameObject.GetComponent<CinemachineVirtualCamera>();
            virtualCamera.Follow = followTarget;
            virtualCamera.LookAt = lookAtTarget;

            virtualCamera.Priority = 1;
        }
        else
        {
            if (virtualCamera != null)
            {
                virtualCamera.Priority = 0;
            }
        }
    }
1 Like