Rotate object by script - rotates only one time

Hello.

Coud you please help me with one problem…

What i am trying to do is rotate an object, with 90 degrees, every time i press the “space”.
Everything works well, but there is one weird problem.
If i rotate the object to 90 degrees manually, before i play the scene and then, Play → hit space, the object rotates only by 90 degrees and stops rotating. This happens only if i rotate the object manualy to 90 or 180 degrees initially in the Editor. If i leave it to 0 or 270 or 360 it rotates, with 90 degrees every time i hit the “space”

Simply attach the script to some object (Cube) and set the custom attributes to:

  • RotateSpeed - 500 // The speed
    for the rotation
  • RotateDegrees - 90 // The amount
    of rotation before it stops
  • RotateDirection - 1 // The direction for the rotation

Play and press the Space bar. Then try the same, but before PLay-ing rotate manually the cube to 90 degrees, Play and press the Spacebar few times. When the cube is initially rotated to 90 or 180 degrees, it rotates only one time and then stops…

 #pragma strict

// Public Variables
public var rotateSpeed		: float;	// Speed of the rotation of the cubes
public var rotateDegrees	: float;	// Rotate degrees interval
public var rotateDirection	: int; 		// Controls the direction of the rotation, -1(clockWise), 1(counterClockWise)

// Private Variables
private var trans			: Transform; // To Store this gameObject
private var shoudRotate		: boolean;	// Tells the cube, that shoud rotate
public var startRotation	: float;	// To store the startRotation
public var curRotation		: float;	// Store the currentRotation

function Start()
{
	trans = this.transform;				// Store this.transform for optimisation
	startRotation = trans.eulerAngles.z;// Store the current rotation
	curRotation = startRotation;		// Store the cur Rotation
	shoudRotate = false;				// Set shoudRotate to false
}

function Update()
{
	if (shoudRotate)		// If the shoudRotate = true
	{
// Get the "MoveTowardsAngle" rotation between curRotation and startRotation + rotateDegrees and stores
// it in "angle".
		var angle : float = Mathf.MoveTowardsAngle(curRotation, startRotation + rotateDegrees, rotateSpeed * Time.deltaTime);
// Use the angle * rotateMultiplied to rotate the object		
    	transform.eulerAngles = Vector3(0, 0, rotateDirection * angle);
    	curRotation = angle;		// Store the rotation amount in curRotation
	}

	if (angle >= startRotation + rotateDegrees) // If the rotation is >= to the startRotation + 90 degrees
	{
		shoudRotate = false;		// Stops the rotation
		if (curRotation >= 360)		// If the rotation goes above 360,
			curRotation = 0;		// we zero it out
			
		startRotation = curRotation;	// Set the startRotation =  CurrentRotation
	}
	
	if (Input.GetKey(KeyCode.Space))
		shoudRotate = true;		// Start rotating the tube
}

You were tying yourself up in knots with angles and floating point accuracy. Here’s a version of your script that rotates your object by 90 degrees (I don’t promise to have respected your conventions on clockwise/anticlockwise motion):

// Public Variables
public var rotateSpeed   : float;    // Speed of the rotation of the cubes
public var rotateDirection  : int;    // Controls the direction of the rotation, -1(clockWise), 1(counterClockWise)

// Private Variables
private var shouldRotate     : boolean; // Tells the cube, that should rotate
public var targetRotation	: Quaternion;	// Where we are headed.

function Start()
{
    shouldRotate = false;          
}

function Update()
{
    if (shouldRotate)   
    {
		var angle : float = Quaternion.Angle(transform.rotation, targetRotation);
		var maxSpin : float = rotateSpeed * Time.deltaTime;
		if (maxSpin >= angle){
			transform.rotation = targetRotation;
			shouldRotate = false;
		}else{
			transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, maxSpin / angle);
		}
    }

    if (!shouldRotate && Input.GetKey(KeyCode.Space)){
    	// Since we know the intended rotation is by 90 degrees, we can do this
    	targetRotation = Quaternion.LookRotation(transform.forward, transform.right * rotateDirection);
       shouldRotate = true;     // Start rotating the tube
    }
}