limit Quaternion.Euler rotation

hey!
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!

mby

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

help

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°.