how to switch between 2 values after 5 seconds using waitforseconds

hi all, i’m lost in yield waitforseconds :frowning: 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;
        }
    }
}

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.

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 :frowning:
    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());
         }
     }