Turret AI rotation gone wrong

Okay so I want my turret to move its Y-axis from 100 to -100 and when it reaches -100 it will go back to 100.

So when I play the game, the turret would rotate up to -100. The ‘rotate’ will increase but gets stuck at -99.

Here’s my code so far…

public var rotate : int = 100;
var rotation : boolean = true;

function Update () {
	if (rotate <= 100 && rotation == true) {
		rotate--;
		if (rotate == -100) {
			rotation = false;
			rotate++;
		}
	}
this.transform.eulerAngles = Vector3(0,rotate,0);
}

There’s a bit of a hole in the logic there. You’re telling it that if it’s less than or equal to 100, it will rotate to -100, but then as soon as it hits negative 100 you call rotate ++ where it will rotate one degree to -99, but then because rotation equals false, it will no longer access the original if statement of which the second one is a part of.

I would do it something like this:

 public var rotate : int = 100;
 var rotation : boolean = true;
 
 function Update () {

	
	//handles switching of false and true rotation
	if (rotate == -100) {
            rotation = false;
         }else if (rotation == 100) {
			rotation = true;
		 }
	
	//handles rotation when rotation is true as denoted by above
	 if (rotate <= 100 && rotation == true) {
		rotate--;
     }
	
	//function called when the rotation is deemed false by the above statement
	if (rotate >= -100 and rotation = false)
	{
		rotate ++;
	}
	    
 this.transform.eulerAngles = Vector3(0,rotate,0);
 }

Thanks! The code needed a little bit of fixing though because there were some misused variables and wrong signs, but nonetheless it works!

Here’s the fixed code: (line 8, it should be rotate and line 18, it should be && and ==)

public var rotate : int = 100;
var rotation : boolean = true;
  
  function Update () {
     //handles switching of false and true rotation
     if (rotate == -100) {
             rotation = false;
          }else if (rotate == 100) {
             rotation = true;
          }
     
     //handles rotation when rotation is true as denoted by above
     if (rotate <= 100 && rotation == true) {
         rotate--;
      }
     
     //function called when the rotation is deemed false by the above statement
     if (rotate >= -100 && rotation == false)
     {
         rotate ++;
     }
         
  this.transform.eulerAngles = Vector3(0,rotate,0);
}