limit Quaternion.Euler rotation

so i got this piece of code when a button gets hold down:

maze.transform.rotation *= Quaternion.Euler(0,0,40*Time.deltaTime);

with this script snippet a plane is rotating smoothly all the time. but i want to let it stop at lets say 20° even if my button is still gets hold down. how can i do this? thanks!


maze.transform.rotation = Quaternion.RotateTowards(maze.transform.rotation, Quaternion.Euler(0,0,40), Time.deltaTime);


I would start by storing your result in a temporary variable, then testing the z-rotation, like so:

Quaternion t = maze.transform.rotation * Quaternion.Euler(0,0,40*Time.deltaTime);

if(t.euler.z > 20){
t = Quaternion.Euler(t.euler.x, t.euler.y, 20);

maze.transform.rotation = t;

This assumes that you want a global limit of 20° rotation, and not that you want the maximum amount of rotation provided by this button to be 20°.