You can use some co-routines to drive your actions here in order to wait a set amount of time. You could alternatively use timers or time value checking. Here I use some general state variables.
SpinControl.js
//Here I assume all of your spinners are tagged *Spinner*
static var changeDuration = 2; //How long to change states in seconds
enum SlotState {Stopped, Starting, Spinning, Stopping};
static var state = SlotState.Stopped;
function OnGUI () {
GUI.Box(Rect(500,10,100,90), "SLOT"); //Make a background box
if(GUI.Button(Rect(510,40,80,20), "Spin") && state == SlotState.Stopped) Spin();
if(GUI.Button(Rect(510,70,80,20), "Stop") && state == SlotState.Spinning) Stop();
}
function Spin() {
state = SlotState.Starting;
for(var spinner : GameObject in GameObject.FindGameObjectsWithTag("Spinner"))
spinner.GetComponent(SpinnerScript).state = SlotState.Starting;
yield WaitForSeconds(changeDuration);
state = SlotState.Spinning;
}
function Stop() {
state = SlotState.Stopping;
//If the order matters, you could do name checking or store the slots as variables
//in this script and then reference them specifically - whatever floats your boat.
for(var spinner : GameObject in GameObject.FindGameObjectsWithTag("Spinner")) {
spinner.GetComponent(SpinnerScript).state = SlotState.Stopping;
yield WaitForSeconds(changeDuration * 0.75); //Slow down part way
}
yield WaitForSeconds(changeDuration * 0.25); //Slow the rest of the way
state = SlotState.Stopped;
}
SpinnerScript.js
//degrees per second
var maxSpeed : int = 15;
var currentSpeed : int = 0;
var state = SlotState.Stopped;
function Update () {
if(state == SlotState.Starting) { //accelerate
currentSpeed += maxSpeed * Time.deltaTime / SpinControl.changeDuration;
if(currentSpeed >= maxSpeed) {
currentSpeed = maxSpeed;
state = SlotState.Spinning;
}
}
else if (state == SlotState.Stopping) { //decelerate
currentSpeed -= maxSpeed * Time.deltaTime / SpinControl.changeDuration;
if(currentSpeed <= 0) {
currentSpeed = 0;
state = SlotState.Stopped;
}
}
transform.Rotate(0, currentSpeed*Time.deltaTime,0);
}
What was with the amtToMove variable that you don't use here?
Since your question title was about a certain number of rotations, I'll assume you meant to use it that way. To stop after a certain number of rotations you could do something like calling the Stop function when the spinner reaches n rotations. Here I do it in the spinner, but you could do it in the SpinControl.
SpinControl.js
//Here I assume all of your spinners are tagged *Spinner*
static var changeDuration = 2; //How long to change states in seconds
enum SlotState {Stopped, Starting, Spinning, Stopping};
static var state = SlotState.Stopped;
function OnGUI () {
GUI.Box(Rect(500,10,100,90), "SLOT"); //Make a background box
if(GUI.Button(Rect(510,40,80,20), "Spin") && state == SlotState.Stopped) Spin();
if(GUI.Button(Rect(510,70,80,20), "Stop") && state == SlotState.Spinning) Stop();
}
function Spin() {
state = SlotState.Starting;
for(var spinner : GameObject in GameObject.FindGameObjectsWithTag("Spinner"))
spinner.GetComponent(SpinnerScript).state = SlotState.Starting;
yield WaitForSeconds(changeDuration);
state = SlotState.Spinning;
}
static function Stop() {
if(state != SlotState.Spinning) return;
state = SlotState.Stopping;
for(var spinner : GameObject in GameObject.FindGameObjectsWithTag("Spinner")) {
spinner.GetComponent(SpinnerScript).state = SlotState.Stopping;
yield WaitForSeconds(changeDuration * 0.75); //Slow down part way
}
yield WaitForSeconds(changeDuration * 0.25); //Slow the rest of the way
state = SlotState.Stopped;
}
SpinnerScript.js
//degrees per second
var maxSpeed : int = 15;
var currentSpeed : int = 0;
var maxRotations : int = 30;
var rotations : int = 0;
var state = SlotState.Stopped;
function Update () {
if(state == SlotState.Starting) { //accelerate
rotations = 0;
currentSpeed += maxSpeed * Time.deltaTime / SpinControl.changeDuration;
if(currentSpeed >= maxSpeed) {
currentSpeed = maxSpeed;
state = SlotState.Spinning;
}
}
else if (state == SlotState.Stopping) { //decelerate
currentSpeed -= maxSpeed * Time.deltaTime / SpinControl.changeDuration;
if(currentSpeed <= 0) {
currentSpeed = 0;
state = SlotState.Stopped;
}
}
transform.Rotate(0, currentSpeed*Time.deltaTime,0);
rotations += currentSpeed*Time.deltaTime / 360;
if(state == SlotState.Spinning && rotations >= maxRotations) SpinControl.Stop();
}