Well. What I think is happening i that the gun rotates then immediate gets out of that “zone” that you described in you if statement, so it jumps back and the process repeats itself. Try adding some Slerp methods or something to smooth it out?
It is doing this because you rotate, then excede the number, then set the rotation back. You need to clamp the number. So its maximum value can only be x and minimum y. It is limited to them.
To do this though, you will need absolute control of your rotation.