Restricting Rotation

Im making a Tilting Labyrinth (Or Super Monkey Ball) style game and have knocked up a simple script to rotate the terrain.

Im hoping somebody will be able to tell me what changes to make to the script so that I can restrict the rotation (in both directions) between two points.

Thanks in advanced for any help.

var rotateSpeed = 3.0;

function Update () { //Rotate around z- axis transform.Rotate(0, 0, Input.GetAxis ("Vertical") * rotateSpeed);

//Rotate around z - axis transform.Rotate(Input.GetAxis ("Horizontal") * rotateSpeed, 0, 0);

}

You could check the euler angles of a rotation and use those for a comparison. It can be tricky, but you can access the euler angles pretty easily:

Debug.Log( "Current Euler Angles: " + transform.eulerAngles );

Put that underneath your 2nd transform.Rotate and you'll get a console log that will tell you the current euler angles. You can use those to write an If statement which you could surround your transform.Rotate's with in your update function.

something like:

function Update ()
{
     if (transform.eulerAngles.z < 90)
     {
         transform.Rotate(0, 0, Input.GetAxis ("Vertical") * rotateSpeed)
     }
}

I don't know if this will exactly work for your situation but it may put you on the right track. Also there are localEulerAngles which might be better depending on your situation. This can be tricky to do right because of wrapping issues from negative degrees to positive degrees and for angles that move past 360.

If you only check the eulerAngles than you have the problem that you start at angle 0 and in the negativ direction you get get 365 instead of -1. This makes it really difficult to set the restriction values.
So make a helper variable like “vRotation” in my case. This can have a positive and negative value, which can be restricted.

For example you want to restrict vertical axis, in my case it is the x axis:

float vRotation = 0;

void FixedUpdate()
{
 float v = Input.GetAxis ("Vertical") * rotateSpeed;
 float angX = transform.eulerAngles.x;

 vRotation += v;

 if(vRotation > 20)
 {
     vRotation =20;
 }
 if(vRotation < -20)
 {
     vRotation =-20;
 }
if(vRotation >-20 && vRotation <20)
{
 transform.Rotate(angX+v,0,0);
}

}

I wrote this from my memory, so I hope i wrote it correctly.