humanoid not aiming properly

I am rotating the chest of my humanoid model and it is acting strange. At normal rotation, the model looks fine. But when aiming up or down, it sort of aims sideways if you can tell in the 2nd picture.136413-capture.jpg

I think it has something to do with the chest being rotated slightly during the aiming animation but I’m not sure. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerscr : MonoBehaviour
{
Animator anim;
Transform chest;
float rotx = 0f;
float roty = 0f;
// Start is called before the first frame update
void Start()
{
anim = GetComponent();
chest = anim.GetBoneTransform(HumanBodyBones.Chest);
}

// Update is called once per frame
void Update()
{
    
}

void LateUpdate()
{
    rotx += Input.GetAxis("Mouse X");
    roty -= Input.GetAxis("Mouse Y");

    chest.rotation *= Quaternion.Euler(roty, rotx, 0);
}

}

In your aiming animation, is everything fine?? (if you don’t apply this script, I mean). You are not saving the last rotation every frame, the next time the bones update, your code in LateUpdate will be overwritten and thus you should store a lastChestRotation Quaternion variable every frame and apply it so as to have this script act as a masking animation of the chest. You should also multiply the other way around:

chest.rotation = Quaternion.Euler(roty, rotx, 0) * chest.rotation;