How to limit z rotation of object in C#

I have a simple flying script. it works. however i am trying to limit the z rotation or “roll” of my object.

it flys around great, however if for example I fly up at a 45 degree angle then turn, the object stays at a rotated angle,
I have tried setting my transform.Rotate(-pitch, yaw, 0); but … it does nothing to change what I have done, I have read the scripting references for Transform.Rotate http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html, but i’m not getting any more info from them.

could someone please point me in the right direction?
I am looking to either auto rotate back into position, or to keep it from “rolling” at all.

this is my code:

using UnityEngine;
using System.Collections;

public class flyscript2 : MonoBehaviour {
	public float AmbientSpeed = 100.0f;
    public float RotationSpeed = 50.0f;
	
	void Update(){
		float roll = 0;
        float pitch = 0;
        float yaw = 0;
		float currot = 0;
		
		//direction
        roll = Input.GetAxis("roll") * (Time.deltaTime * RotationSpeed);
        pitch = Input.GetAxis("pitch") * (Time.deltaTime * RotationSpeed);
        yaw = Input.GetAxis("yaw") * (Time.deltaTime * RotationSpeed);
		transform.Rotate(-pitch, yaw, -roll);
		
		//forward
		Vector3 AddPos = Vector3.forward;
        AddPos = rigidbody.rotation * AddPos;
        rigidbody.velocity = AddPos * (Time.deltaTime * AmbientSpeed);

    }
}

thank you,
gathos.

hi
you want to limit z rot between 2 values?
stop the rotation when no axis is used?

Maybe you want to use a Quaternion lerp. Then when the plane has finished, give it a quaternion to go back to it’s start position over time, if the stick or whatever is no longer giving any value.

When you do
transform.Rotate(-pitch, yaw, 0);

Is it after you did
transform.Rotate(-pitch, yaw, -roll);
or instead ?

Because if it is after it won’t change anything, you are just applying another transform on top of the previous one.

What you can try to do instead is checking the yaw value, making sure you don’t go outside of the desired value :

yaw = Input.GetAxis(“yaw”) * (Time.deltaTime * RotationSpeed);
if(transform.localEulerAngles.z > max_Value || transform.localEulerAngles.z < min_Value ) yaw = 0f;

and THEN
apply your rotation with :

transform.Rotate(-pitch, yaw, -roll);

You can try this otherwise :

float roll = transform.localEulerAngles.y + Input.GetAxis(“roll”) * (Time.deltaTime * RotationSpeed);
float pitch = transform.localEulerAngles.x + Input.GetAxis(“pitch”) * (Time.deltaTime * RotationSpeed);
float yaw = transform.localEulerAngles.z + Input.GetAxis(“yaw”) * (Time.deltaTime * RotationSpeed);

// You clamp your Z value;
roll = Mathf.Clamp (roll, min_Value, max_Value );

// Update the localEurlerAngles
transform.localEulerAngles = new Vector3(-pitch, yaw, -roll);