Bone rotation overrides animations completely

So I am making a FPS multiplayer game and got stuck at synchronizing animations. After delving into why it didn’t work, I have noticed that it is because of mouse look and bone rotation.

I had this problem before and did find the solution that worked back then. Weirdly, it no more works for me. Other threads/posts didn’t really help or were different topics, or were too old.

I can confirm that the animations do work, the model is rigged up correctly and nothing else interferes. It seems only the bone rotation messes up the animation and it simply doesn’t play, because they started to work and sync across clients after I commented up the script part.

In short, if I’m rotating the bones with script, animations don’t play, if I don’t rotate, they play. I suspect I have some kind of bug in my code.

Here is the animation controller script I use:

private float rotationX;
private float rotationY;
private float rotationZ;

void LateUpdate()
    {
        BodyPartRotation(rotationX);
        if (!isLocalPlayer)
        {
            return;
        }
        var newrotationX = -m_Camera.transform.eulerAngles.x;

        CmdBodyPartRotation(newrotationX);    
    }

    [Command]
    void CmdBodyPartRotation(float newrotationX)
    {
        BodyPartRotation(newrotationX);
        RpcBodyPartRotation(newrotationX);
    }

    [ClientRpc]
    private void RpcBodyPartRotation(float newrotationX)
    {
        if (!hasAuthority)
        {
            BodyPartRotation(newrotationX);
            return;
        }
        BodyPartRotation(newrotationX);
    }

    void BodyPartRotation(float newrotationX)
    {
        rotationX = newrotationX;

        Vector3 newRot = new Vector3(newrotationX, 0, 0);
        newRot = newRot + armLpos;
        armL.localEulerAngles = newRot;
        newRot = new Vector3(newrotationX, 0, 0);
        newRot = newRot + armRpos;
        armR.localEulerAngles = newRot;
        newRot = new Vector3(0, 90, -newrotationX);
        head.localEulerAngles = newRot;
    }

So it seems instead of

    Vector3 newRot = new Vector3(newrotationX, 0, 0);
    newRot = newRot + armLpos;
    armL.localEulerAngles = newRot;
    newRot = new Vector3(newrotationX, 0, 0);
    newRot = newRot + armRpos;
    armR.localEulerAngles = newRot;
    newRot = new Vector3(0, 90, -newrotationX);
    head.localEulerAngles = newRot;

I did

    armL.localEulerAngles += new Vector3(0, -newrotationX, 0);
    armR.localEulerAngles += new Vector3(0, -newrotationX, 0);
    head.localEulerAngles = new Vector3(0, -newrotationX, 0);

Which apperantly works, but only for the client. It starts to jitter all over the place.

So, instead of telling all clients to rotate the bone, I instead made it to change the angle of the bone in the ClientRPC method.

I’m not sure if it is a good thing to do, but it seems to work flawlessly like that.

So, in other words (or in code)

 private float rotationX;
 private float rotationY;
 private float rotationZ;
 
 void LateUpdate()
     {
         
         if (!isLocalPlayer)
         {
			 //new!
		     //we moved this into here because we aren't going to use rotationX for the local client
			 BodyPartRotation(rotationX);
             return;
         }
         var newrotationX = -m_Camera.transform.eulerAngles.x;
 
	     //new!
	     //we instead put here, while using the newrotationX, because htis is the local client
		 BodyPartRotation(newrotationX);
 
         CmdBodyPartRotation(newrotationX);    
     }
 
     [Command]
     void CmdBodyPartRotation(float newrotationX)
     {
         RpcBodyPartRotation(newrotationX);
     }
 
     [ClientRpc]
     private void RpcBodyPartRotation(float newrotationX)
     {
         if (!hasAuthority)
         {
			 //new!
			 //change the value instead of change the bone.
             rotationX = newrotationX;             
         }
     }
 
     void BodyPartRotation(float newrotationX)
     {
        var pos = new Vector3(0, -newrotationX, 0);

		//new!
		//instead of changing, we add it to the actual value
        armL.localEulerAngles += pos;
        armR.localEulerAngles += pos;
        head.localEulerAngles = pos;

		//I moved this to the bottom of the method just for the sake of it.
        rotationX = newrotationX;
     }