Clamping transform.Rotate on the z-axis

Hello!

I’m still pretty new to coding, and I haven’t seemed to come up with a solution to this little problem I have. Its a cannon that rotates up and down to
I want to clamp a rotation on one axis for a 2D game so that its not able to spin all the way round.

void Update () {

if (angleUp)
{
    transform.Rotate(0, 0, -speed);
}
if (angleDown)
{
    transform.Rotate(0, 0, speed);
}

I’ve tried clamping the rotation using transform.rotation.z = Mathf.Clamp(transform.rotation.z, x,y)
But I get an error “Cannot modify the return value of ‘Transform.rotation’ because it is not a variable”

and I’ve tried clamping the euler angles, which has just stopped the rotation entirely?

There doesn’t seem to be a quick solution to this issue?

You need to clamp the rotation components (x, y, z and w). transform.rotation is a quaternion, for which I believe you’d need to define a custom function to clamp as Vector4 doesn’t have a clamp function defined for what you need.