I am working on an animation that rotates an object by 60 degrees. I was thinking of delaying the rotation every 10 degrees by 0.25 seconds. All I need is a delay function between statements. When ever I try to find this answer, people talk about coroutines, yield waitforseconds, but nothing works and can go between statements. How can I simply delay it for a certain amount of time between statements?
My Code:
void Move(){
Vector3 v31 = new Vector3 (0, 0, 0);
Vector3 v32 = new Vector3 (0, 0, 3);
tf.RotateAround(v31, v32, 10);
//delay
tf.RotateAround(v31, v32, 10);
//delay
tf.RotateAround(v31, v32, 10);
//delay
tf.RotateAround(v31, v32, 10);
//delay
tf.RotateAround(v31, v32, 10);
//delay
tf.RotateAround(v31, v32, 10);
}
If I were you I would just add a little timer, here ill give you an example
float timeInterval = 0.25f; //Or whatever time interval you want
float timer = 0;
void Start() {
timer = timeInterval;
}
void Update() {
timer -= Time.deltaTime;
}
void Move(){
Vector3 v31 = new Vector3 (0, 0, 0);
Vector3 v32 = new Vector3 (0, 0, 3);
tf.RotateAround(v31, v32, 10);
if(timer < 0) {
timer = timeInterval;
tf.RotateAround(v31, v32, 10);
}
if(timer < 0) {
timer = timeInterval;
tf.RotateAround(v31, v32, 10);
}
if(timer < 0) {
timer = timeInterval;
tf.RotateAround(v31, v32, 10);
}
if(timer < 0) {
timer = timeInterval;
tf.RotateAround(v31, v32, 10);
}
if(timer < 0) {
timer = timeInterval;
tf.RotateAround(v31, v32, 10);
}
}
Thats the simplest way you could do that,
if you want a more detailed one just ask and ill see what I can do
Good luck
NightLucidity
I would use InvokeRepeating to achieve this:
int numOfTimes = 6; //The number of times to rotate by 10
void Start(){
InvokeRepeating("Move", 0.25f,0.25f); //This calls Move every 0.25 seconds. This can be called from wherever you want.
}
void Move(){
if(numOfTimes != 0)
{
Vector3 v31 = new Vector3 (0, 0, 0);
Vector3 v32 = new Vector3 (0, 0, 3);
tf.RotateAround(v31, v32, 10);
numOfTimes -= 1;
}
else{
CancelInvoke("Move"); //Stops Move from being called evey 0,25 seconds
}
}
NOTE: Not tested…
Aside from using Mecanim to animate this, I would agree that Coroutines are your best bet. you are possibly having trouble getting Coroutines working as you might be attempting to call them in a single function. Coroutines operate best as a standalone function that must have the return type IEnumerator to work correctly.
here is an example code snippet of your situation using coroutines
Vector3 v31 = new Vector3 (0, 0, 0);
Vector3 v32 = new Vector3 (0, 0, 3);
Coroutine handle;
public void Move()
{
CancelMove(); //if already moving, stop current coroutine so you could restart it
handle = StartCoroutine(MoveRoutine());
}
void CancelMove()
{
if (handle != null)
{
StopCoroutine(handle);
}
}
IEnumerator MoveRoutine()
{
for (int iter=0; iter<6; iter++)
{
transform.RotateAround(v31, v32, 10);
yield return new WaitForSeconds(0.25f);
}
}
You can publically (or privately, all matter how you want to access this) just call Move() and the rest is handled internally.
It is easy for someone to misunderstand how Coroutines can work for someone who has never worked with a similar concept before as they are a tad different from how a normal function operates. When you reach a “Yield” in a coroutine the compiler simply stops and does the rest of the function later after a specific trigger has been met (waiting for a number of seconds, for a different coroutine to finish or even on the next frame). so when you reach that Yield statment in the MoveRoutine the function is basically placed on standby for 0.25 seconds before it goes through the next iteration of the for loop. In the meantime the compiler is free to do other things while the coroutine/function is in its “micro-hibernation”
CancelMove() and the variable “handle” aren’t really needed to get this to work but grant you the power to cancel the coroutine if you ever needed to.