how to switch between 2 values after 5 seconds using waitforseconds
hi all, i'm lost in yield waitforseconds :( i try to change the y rotation from +90 to -90 every 5 seconds using an IEnumerator when a boolean (is_attacked) = true. dont know if the IEnum.. doesnt work or the way how to set and get the values then in fixedupdate. hope so much someone will help me to get through. P.S: this yield thing is soooo complicated for beginners.
this is my script:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float thrust;
public Rigidbody rb;
public float yRotation = 0.0F;
public bool is_attacked = false;
public bool turned_left = false;
public bool turned_right = false;
void Start()
{
rb = GetComponent<Rigidbody>();
}
IEnumerator Wait()
{
yield return new WaitForSeconds(5);
}
void Update()
{
if (yRotation >= 360.0F)
{
yRotation = 0.0F;
}
if (yRotation <= -360.0F)
{
yRotation = 0.0F;
}
// yRotation += Input.GetAxis("Horizontal");
}
void FixedUpdate()
{
y_rotation();
transform.eulerAngles = new Vector3(0, yRotation, 0);
rb.AddRelativeForce(Vector3.forward * thrust);
rb.velocity = transform.forward * rb.velocity.magnitude;
}
IEnumerator y_rotation()
{
if (is_attacked == true)
{
yRotation = -90.0f;
yield return StartCoroutine(Wait());
yRotation = 90.0f;
}
}
}
Answer by pekalicious · Nov 28, 2015 at 10:51 PM
y_rotation();
Should be
StartCoroutine(y_rotation());
Just like you do for Wait(); Keep in mind that when you are not in a coroutine (basically a method that returns IEnumerator) you cannot yield. So you simply call StartCoroutine. If you are in a coroutine, you can either call 'StartCoroutine', or 'yield return StartCoroutine'. The difference is that the latter will wait for that coroutine to complete before continuing, while the former will not.
There are a lot more to it than this, so you might want to check out some tutorials by googling.
Answer by Ego65 · Nov 29, 2015 at 05:45 AM
many thx peka ! i've tried that but now i've got a strange behavior. seems that coroutine is executed as it should only for one (the very first) duration. after that it becomes a mess.
it behaves this way :
when is_attacked has become true, yrotation = -90. The ships turns left and moves in the new direction as expected
after 5 sec yrotation = +90 as expected but now the ship doesnt react anymore and doesnt change it's direction
Then it seems the 5 sec interval becomes a mess and the yrotation is switched in random time intervals(sometimes immediately or in 1 sec or sometimes in more than 5 sec as well as the ship doesn't react.
crazy :( so i'v tried to set it in a while loop but this didn't help
IEnumerator y_rotation() { while (is_attacked == true) { yRotation = -90.0f; yield return StartCoroutine(Wait()); yRotation = 90.0f; yield return StartCoroutine(Wait()); } }
sure i tried to read tuts and in unity answers help topics for that but to read and understand there, is one thing. But to read and understand and adapt it to the own needing is very much more difficult, as well i'm german my (school english isn't that bad but in such special terms it makes it double difficult for me. so hopefully you'll be patientely with such a poor ol' german beginner in game progra$$anonymous$$g with unity, as i am :)
many thx previously
yours
That's probably because you are starting the coroutine every single physics update, so you are likely running thousands of coroutines at the same time. Frankly, I'm surprised your machine hasn't exploded.
If I'm understanding correctly, what you want to do is just have your game object rotate every time it's in attack mode, whenever that happens, otherwise don't do anything.
A quick and dirty alternative would be to have a single coroutine execute forever, and when it detects you are in attack mode, then execute your rotation logic.
IEnumerator y_rotation()
{
while (true) // run this forever and ever
{
if (is_attacked == true) // if in attack mode, do rotation logic
{
yRotation = -90.0f;
yield return StartCoroutine(Wait());
yRotation = 90.0f;
yield return StartCoroutine(Wait());
}
yield return null; // wait a single frame to let Unity breath
}
}
Then, ins$$anonymous$$d of calling this coroutine in FixedUpdate, call it once in Start.
that has been basically exactly that what i wanted to achiefe.
thank you very much !
me too, thats why i like my machine :)Frankly, I'm surprised your machine hasn't exploded
now i have to figure out how to lerp the yrotation as it did with Input.GetAxis("Horizontal") that the moving of the enemy is more realistic
Your answer
![](https://koobas.hobune.stream/wayback/20220612050654im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
yeild return new WaitForSeconds doesn't work 1 Answer
Question to WaitForEndOfFrame 2 Answers
Invoke method is not called 2 Answers
WaitForSeconds clarification 1 Answer
How do I write yield WaitForSeconds(1.0); from Javascript in C# 0 Answers