Why does my recoil code 'spaz' out?

This is some recoil code for my gun and what its supposed to do is make the gun go up 10 degrees when the “Fire 1” button is pressed but what actually happens is my gun Spazzes out(Theres no better way of describing it), Heres my code:

public class Theshoot : MonoBehaviour {

 public Rigidbody bulletPrefab;

 public Transform barrelEnd;

public GameObject gun;

 public float Recoil = 10f; 

private Vector3 angle =new Vector3(0,0,0);

private Vector3 recoil = new Vector3(-10,0,0); //You'll have to test for these values
	
	private Vector3 newAngle = Vector3.zero;
	void Update ()
    {
        
		gun.transform.localEulerAngles = Vector3.Slerp(gun.transform.localEulerAngles, angle, Time.deltaTime * 5);

		
		if (Input.GetButtonDown("Fire1"))
        {
			Rigidbody rocketInstance;
            rocketInstance = Instantiate(bulletPrefab, barrelEnd.position, bulletPrefab.transform.rotation) as Rigidbody;
            rocketInstance.AddForce(barrelEnd.forward * 10000);
			gun.transform.localEulerAngles += recoil;

I’ve had what seems like exactly the same issue when using Mecanim. I tried to animate a character’s arm between 0 and -10 degrees - a slight swing - and instead it animated it between 0 and 710 degrees. Positive values were no problem, but for negative values it went nuts. Since that probably uses the same Slerp as the Unity engine, this suggests to me that there’s a fault in that. Try changing that to Lerp, or implementing the interpolation yourself.

Edit: Actually, when I think about exactly how this problem occurred in Mecanim, I’d say that localEulerAngles is the problem, not slerp (although still use lerp instead of slerp, as explained in the comment below). Avoid accessing transform.localEulerAngles if possible.

In fact, when I look at the documentation, it seems this issue is known about, and a workaround is suggested (though I’m still not sure how to work around it in Mecanim, but never mind). About transform.localEulerAngles, it says:

Only use this variable to read and set
the angles to absolute values. Don’t
increment them, as it will fail when
the angle exceeds 360 degrees. Use
Transform.Rotate instead.

When you lerp from the current localEulerAngle to -10, supposing it starts off at 0, it might go to -1 this frame. But when you read it the next frame, it might read as 359 instead of -1, so then it’s trying to go from 359 to -10, and so on. So make a private variable (probably a vector3), lerp that instead (your own vector can go from 0 to -10 without getting altered behind the scenes) and use that vector to update localEulerAngles each frame.