localEulerAngles causing problems with gun X rotation

Hi there,

I am having an issue with the localEulerAngles of my player game object wrapping around through the range 0-360. I have the gun x rotation calculated by the localEulerAngles of the object so that when the player looks from rotation 0 and then rotates in the negative direction, the rotation of the player jumps from 0 to 360…hence causing the gun to do a full rotation.

This looks really strange as the gun rotates in the opposite direction to line up with the player rotation.

This is the code segment that handles the rotation of the player:

 rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
 transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);

The players rotation is fetched from the gun scripts using

 playerView.GetComponent<MouseLook>().rotationX.

I’ve tried a lot of different things but I haven’t found a way to fix it yet.

Any help will be much appreciated, thanks!

Try replacing your first two-lines snippet with the following :

transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
rotationX = transform.localEulerAngles.y;