rotating an object only on z-axis help - c#

I’m attempting to adjust the barrel of a gun up and down (to show it’s going fire a further distance)

I can get it to rotate just fine, but i only want it to rotate with in a certain radius otherwise you can just keep flipping it.

if  (Input.GetKey(KeyCode.S) && gun.transform.rotation.z >= 350 || gun.transform.rotation.z <= 20))
gun.transform.RotateAroundLocal(Vector3.back, (rotateRate * Time.deltaTime) * -1f);

this doesn’t seem to work. I thought maybe the rotation would be in radians (that broke things)
so i believe my math is messed up…

so this is what i want to do, have a gun that moves down when you press S and if it goes to 350 (after wrapping past 0,) it stops…

on the other side, i would like to do the opposite with W where if it goes from 350 (wrapping past 0) to 20 it stops at 20.

The default rotation vector is interpreted as a quaternion, which is better for the maths but really hard to visualize. Try localEulerAngles instead, which is the XYZ rotation relative to the parent object:

Vector3 rot = gun.transform.rotation.localEulerAngles;
if  (Input.GetKey(KeyCode.S) && rot.z >= 350 || rot.z <= 20))
    rot.z -= rotateRate * Time.deltaTime;
gun.transform.rotation.localEulerAngles = rot;

You may also need to handle wrapping if the Z exceeds 360 or drops below 0, because it won’t do it for you.

i’ve changed how i am checking my code, i’m not using a second rotation to set it, cause it’s not catching it, i also threw in some variables to check for wrapping. I also built in a boolean catch to try and stop it, some time it works some times i can get past it, some times it doesn’t catch it at all…

this is my full update code for it.

if(Input.GetKeyDown(KeyCode.W))
		{
			rotate = true;
		}
		if (Input.GetKeyDown(KeyCode.S))
		{
			rotate = true;
		}
		if (rotate)
		{
	
			if  (Input.GetKey(KeyCode.S) && (gun.transform.rotation.eulerAngles.z >= 350 || gun.transform.rotation.eulerAngles.z <= 20))
			{
					if (wrapped == false && tooFar == true && Input.GetKey(KeyCode.S))
					{
						tooFar = false;
					}
					if (gun.transform.rotation.eulerAngles.z <= 0 && Input.GetKey(KeyCode.S))
					{
						wrapped = true;
					}
					if (gun.transform.rotation.eulerAngles.z <= 350 && wrapped == true && Input.GetKey(KeyCode.S))
					{
						tooFar = true;
					}
					else
					{
						tooFar = false;
					}
					
					if (tooFar == false)
					{
						gun.transform.RotateAroundLocal(Vector3.forward, (rotateRate * Time.deltaTime) * -1f);
					}
					if (tooFar == true)
					{
						rotate = false;
					}
			}
			else if  (Input.GetKey(KeyCode.W) && gun.transform.rotation.eulerAngles.z <= 350 || gun.transform.rotation.eulerAngles.z >= 20)
			{
					if (gun.transform.rotation.eulerAngles.z >= 359 && Input.GetKey(KeyCode.W))
					{
						wrapped = false;
					}
					if gun.transform.rotation.eulerAngles.z >= 20 && Input.GetKey(KeyCode.W) && wrapped == false) 
					{
						tooFar = true;
					}
					else
					{
					tooFar = false;
					}
					if (tooFar == false)
					{
						gun.transform.RotateAroundLocal(Vector3.back, (rotateRate * Time.deltaTime) * -1f);
					}
					if (tooFar == true)
					{
						rotate = false;
					}
			}
			else
			{
				rotate = false;
			}
		}
	}

still could use some help on this, i’m not sure if i should use the second vector as i never caught anything with trying it.

thanks!