WaitForSeconds can only pause coroutines - when you call a coroutine, it returns almost immediately, but continues running in the background each frame. If you want to pause between rotations, do the whole job in a coroutine - like this:
function Start(){ // Start can be a coroutine
while (true){
yield WaitForSeconds(2);
transform.Rotate(0,90,0, Space.Self);
}
}
NOTE: Your original code is starting a new coroutine each Update - at 60 frames per second, there will be about 120 LookAround coroutines running at the same time!
NOTE 2: After some time, the object may be rotating around some weird axis - this happens because successive Rotate calls may accumulate errors. A better solution would be to set eulerAngles directly (the rigidbody follows the transform orientation):
function Start(){ // Start can be a coroutine
var euler = transform.eulerAngles;
while (true){
yield WaitForSeconds(2);
euler.y = (euler.y + 90)% 360; // rotate euler.y modulo 360
transform.eulerAngles = euler;
}
}
EDITED: If you want to control the direction, use a member variable:
var dir: int = 1; // member variable
function Start(){ // Start can be a coroutine
var euler = transform.eulerAngles;
while (true){
yield WaitForSeconds(2);
euler.y = (euler.y + dir * 90)% 360; // rotate euler.y according to dir
transform.eulerAngles = euler;
}
}
function Update(){
if (Input.GetKeyDown("right")) dir = 1;
if (Input.GetKeyDown("left")) dir = -1;
}
Remember that a coroutine returns almost immediately (basically, when it encounters the first yield) and is resumed automatically each frame, thus the Start coroutine will return despite the infinite loop.
EDITED 2: In this particular case, maybe a more traditional Update solution would be better: have a timeToRotate variable, which shows the next time to rotate; in Update, if the up arrow key is pressed, set rigidbody.velocity and reload timeToRotate to some interval (this will postpone the next rotation forever while the up key is pressed); if not pressed, stop the rigidbody and check timeToRotate: when it’s reached, rotate and reload timeToRotate to time + desired interval.
This is a much more versatile solution: you can stop rotations and move while the key is pressed, and have different intervals for the first rotation after Start, the first rotation after releasing the key and for any rotation while key not pressed:
var dir: int = 1;
var moveSpeed: float = 10;
private var timeToRotate: float; // shows the time for next rotation
private var euler: Vector3; // save initial eulerAngles
// set the next rotation time to "interval" seconds from now:
function ReloadTime(interval: float){
timeToRotate = Time.time + interval;
}
function Start(){
euler = transform.eulerAngles;
ReloadTime(4.5); // rotations will begin 4.5 seconds after Start
}
function Update(){
if (Input.GetKey("up")){ // while up arrow pressed...
rigidbody.velocity = transform.forward * moveSpeed; // move forward at moveSpeed...
ReloadTime(1.2); // and postpone the new rotation time by 1.2 seconds
} else { // up arrow not pressed:
rigidbody.velocity = Vector3.zero; // stop the rigidbody...
if (Time.time >= timeToRotate){ // and if timeToRotate has come...
euler.y = (euler.y + dir * 90)% 360; // rotate euler.y according to dir...
transform.eulerAngles = euler;
ReloadTime(2.0); // and set next time to 2.0 seconds from now
}
}
}