programing problem

hi, being a beginner i am unable the tackle simple programming problem, and i am wondering if anyone could help me with this.

i have made a script that makes camera rotate around object similiar to the unity3d gizmo in the top-right corner. it works fine but i am using a lot of if statements to check where is the value that represent rotation, in what quadrant. so basically if i have a x value that represent rotation of the camera around x axis is,for example, x=41 i have a sequence of if statements that will determine that x value is between 0 and 90 and then lerp x value towards 90...

is there a better way of doing it, for example is there a simple way of determining the nearest number to x value if i have these values for checking 0,90,180,270 and 360?

thanks!

here is the code i have so far (this is attached to the button that controls rotation to the left)

//REFERENCE TO CAMERA
var cam : MainCamera;

function Start(){

    //GET THE REFERENCE TO THE CAMERA
    cam=GameObject.Find("MainCamera").GetComponent("MainCamera");

}

function OnMouseDown(){

    var xRotValue=cam.GetXRot();
    Debug.Log("x: " + xRotValue);

    if(xRotValue<0){
        xRotValue=xRotValue+360;
        //HERE WE NEED TO SET THE ACTUALL X VALUE FOR THE CAMERA
        //RIGHT THE FUNCTION THAT WILL MAKE THAT POSSIBLE
        cam.SetXRot(xRotValue);

    }

    if(xRotValue>360){
        xRotValue=xRotValue-360;
        //HERE WE NEED TO SET THE ACTUALL X VALUE FOR THE CAMERA
        //WRITE THE FUNCTION THAT WILL MAKE THAT POSSIBLE
        cam.SetXRot(xRotValue);

    }

    if(xRotValue<=270 && xRotValue>180){

        Debug.Log("we should lerp to 180" + "current x: " + cam.GetXRot());
        cam.LeftRotate(180);

    } 

    if(xRotValue<=180 && xRotValue>90){

        Debug.Log("we should lerp to 90" + "current x: " + cam.GetXRot());
        cam.LeftRotate(90);

    }

    if(xRotValue<=90 && xRotValue>0){

        Debug.Log("we should lerp to 0" + "current x: " + cam.GetXRot());
        cam.LeftRotate(0);

    }

    if(xRotValue<=360 && xRotValue>270){

        Debug.Log("we should lerp to 270" + "current x: " + cam.GetXRot());
        cam.LeftRotate(270);

    }

    if(xRotValue<=0){

        Debug.Log("we should lerp to 270" + "current x: " + cam.GetXRot());
        cam.LeftRotate(-90);

    }

}

function LeftRotate is in the camera script:

function LeftRotate(ToRotation : float){

        var t=0.0;
        while (t < 1.0) {                  

        t += Time.deltaTime * (1.0/0.2); 

        x=Mathf.Lerp(x,ToRotation,t);
        y=Mathf.Lerp(y,0,t);
        yield;

    }

}

Have a look at this answer to a similar question:

http://answers.unity3d.com/questions/14907/rounding-rotation-to-nearest-90-degrees/14922#14922

I would do something like this:

var lastToRotation = -1.0;

function ApproximatelyEqual(a : float, b : float, threshold : b)
{
    return (Mathf.Abs(a - b) <= threshold);
}

function LeftRotate(ToRotation : float){

        var t=0.0;
        while (t < 1.0 && ApproximatelyEqual(ToRotation, lastToRotation, 0.5)) {                  

        t += Time.deltaTime * (1.0/0.2); 

        x=Mathf.LerpAngle(x,ToRotation,t);
        y=Mathf.Lerp(y,0,t);
        yield;
    }
}

And in your other script

function OnMouseDown(){

    var xRotValue=cam.GetXRot();
    Debug.Log("x: " + xRotValue);
    var xRotInt = xRotValue / 90;
    xRotValue = xRotInt * 90;
    cam.LeftRotate(xRotValue);
}

That should stop it from running multiple coroutines at the same time, and will always go to the value at the bottom of the current quadrant. Also, use LerpAngle for lerping angles

with kind help of person from the forums i solved this simple problem with this function

function findClosestAngle( angle ) { return 90 * Mathf.RoundToInt( angle / 90.0 ); }

it will return 90 if the angle is bigger then 45 and 0 if the angle is less then 45