help with programming logic

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 guess there is a better way of doing it. here is the code:

function RightRotate(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;
			   
 }

}

x and y represents rotations by x and y axis. this function takes the end point of the rotation. that could be 0,90,180 and 270. i am also converting 360 to 0 and so on…

i am using this function like this:

 if(xRotValue<270  xRotValue>=180){
   cam.LeftRotate(270);
 } 
	
 if(xRotValue<180  xRotValue>=90){
   cam.LeftRotate(180);
 }

for all values and combinations that i can think of…so that is bad. it works though. can it be done in a simplier way. for example to try to calculate the degrees to rotate the camera by the nearest point (0,90,180 270 or 360). how to calculate that??

i guess this is simple to people who know to program well…thanks!

it has to be something simplier…

to simplify even further…
how can i determine what is the closest number from this array: 0,90,180,270,360 when i have a value between 0-360

so if i have x=63 how to deterimine easily what are the closest numbers to it (in this case that would be 0 and 90)

Hi Pretender!

function findClosestAngle( angle )
{
	return 90 * Mathf.RoundToInt( angle / 90.0 );
}
// Here's some tests to prove it works!
Debug.Log( findClosestAngle( 2 ) );
Debug.Log( findClosestAngle( 56 ) );
Debug.Log( findClosestAngle( 101 ) );
Debug.Log( findClosestAngle( 163 ) );
Debug.Log( findClosestAngle( 190 ) );
Debug.Log( findClosestAngle( 250 ) );
Debug.Log( findClosestAngle( 300 ) );
Debug.Log( findClosestAngle( 350 ) );

The trick here is that since we know you are looking for multiples of 90, you can divide the angle you have by 90 and round that to the nearest integer. That will tell you “which 90” was closest. Then you just multiply to get the value you want.

EDIT: I just slapped that together, you’ll probably want more descriptive function names.

Hope that helps!
-Howard

it works great, thank you very much!