can anyone help me stop a rigidbody from rotating around its z axis

I have been working on creating a script that rotates a object using the mouse along it’s y and x axis but i want it’s z axis to return to zero after turning, does any one have any suggestions.

this is what i have so far.

  #pragma strict
    
    var xspeed = 4.0;
    var yspeed = 4.0;
    //var zspeed = 4.0;
    
    
    
    function FixedUpdate ()
    {
    //Ignore this stuff it was leftovers from my earlier attempts//
    //	var z : float;
    //	if(transform.eulerAngles.z > 0.5f)
    //	{
    //		z = -zspeed;
    //	}
    //	if(transform.eulerAngles.z < -0.5f)
    //	{
    //		z = zspeed;
    //	}
    	
    	var x = xspeed * Input.GetAxis("Mouse Y");
    	var y = yspeed * Input.GetAxis("Mouse X");
    	rigidbody.AddTorque(x, y, z);
    //	Debug.Log(z);
    	
    }

A better option is to use Transform.Rotate(x,y,z) since using torque does not give you instant rotation as you have to wait for angular velocity to increase.

Your code should be like this:

var x = xspeed * Input.GetAxis("Mouse Y");
var y = yspeed * Input.GetAxis("Mouse X");
    
transform.Rotate(xspeed,yspeed,0);

If you use a rigidbody u can constrain any axis rotation using inspector if you dont want any unwanted behaviour

Hope that helps…