I feel stupid asking this, but i’m not sure how its done.
I would like an object to rotate 180 degrees on it’s x axis after waiting 3 seconds, and then repeat. But when I use this script, it just waits three seconds on load time, then continues spinning forever. Sorry for this stupid question
function spin(){
yield WaitForSeconds(3.0);
transform.Rotate(0, 0, 180 * Time.deltaTime);
}
function Update(){
spin();
}
I guess I want the spin function to loop, but I don’t know how.
Kith
2
If I understand the question, what you want is to have the object finish a rotation of 180 degrees every 3 seconds? What your code is doing now is initially waiting for 1 second (your yield statement), and then spinning 180 degrees per second after that. I think what you want would look something like this.
function spin() {
transform.Rotate(0,0,60*Time.deltaTime)
}
function Update() {
spin();
}
What’s happening here is that your rotating the object 60 degrees per second (which is the same thing as saying 180 degrees every 3 seconds).
Hope that’s what you were looking for!
-Kith
That’s the old fashioned way to do this:
private var tCycle:float;
function Update(){
var t = Time.time;
if (t>tCycle) tCycle = t+3;
if (tCycle-t<=1){ // Spins during the last second
transform.rotate(0,0,180*Time.deltaTime);
}
}
I’m sure yield could be a better alternative, but I just can’t understand this %#@! thing!
If you’re just want a nice demo, then you can use this script:
IEnumerator spin(){
while(true){
yield return new WaitForSeconds(2.0f);
float timer = 0f;
while(timer<1){
transform.rotate(0,0,180*Time.deltaTime);
timer = timer + Time.deltaTime;
yield return null;
}
}
}
void Start(){
StartCoroutine(spin());
}
But, if you want precidion in rotation, some changes should be applied:
IEnumerator spin(){
while(true){
yield return new WaitForSeconds(2.0f);
float timer = 0f;
Quaternion lastRotation = transform.rotation;
while(timer<1){
transform.rotate(0,0,180*Time.deltaTime);
timer = timer + Time.deltaTime;
yield return null;
}
transform.rotation = lastRotation;
transform.rotate(0, 0, 180);
}
}
void Start(){
StartCoroutine(spin());
}
Hope it helps!