Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Camera Rotation over server does not work when trying to clamp the movement on the x axis.

Discussion in 'Multiplayer' started by mumbouk, Oct 31, 2022.

  1. mumbouk

    mumbouk

    Joined:
    Jun 23, 2014
    Posts:
    3
    I am having an issue where when I attempt to clamp the rotation of my player over the server it does not work.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Unity.Netcode;
    6.  
    7. public class CamLook : NetworkBehaviour
    8. {
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (!IsOwner) return;
    14.         float mouseY = Input.GetAxis("Mouse Y");
    15.         CamLookServerRpc(mouseY);
    16.     }
    17.     [ServerRpc]
    18.     void CamLookServerRpc(float mouseY)
    19.     {
    20.         CamLookClientRpc(mouseY);
    21.     }
    22.     [ClientRpc]
    23.     void CamLookClientRpc(float mouseY)
    24.     {
    25.         transform.Rotate(-mouseY, 0, 0);
    26.     }
    27. }
    28.  
    This is the code without attempting to use mathf.clamp to stop the camera moving too far on the x rotation. If I add the mathf.clamp function, it does not work.
    mouseY = Mathf.Clamp(mouseY, -90f, 90f);

    I try to fix this by changing:
    transform.Rotate(-mouseY, 0, 0);

    to
    transform.rotation = Quaternion.Euler(-mouseY, 0f, 0f);

    which results in the camera only rotating a little amount and then instantly being put back to the original position as if it were pulled back by a rubber band.