0
\$\begingroup\$

I'm trying to flip a sprite on the X-axis continuously at a certain delay. That is, when the game starts the enemy sprite should keep flipping until destroyed.

I first tried adding it via animator but for some weird reason FlipX via animator was not working, scale and rotation would work but FlipX would have no effect. Maybe some bug?

Then I tried adding it as a script. I did some reading here and found that it can either be done via Invoke() or Coroutine(). I wrote the script below from examples but it is not working. It flips the sprite at start and then stops. How do I make it flip continuously?

private bool flipState;

    // Use this for initialization
    void Start () {
        flipState = false;
        StartCoroutine("Flippy");

    }

    void Update()
    {

    }



    IEnumerator Flippy()
    {
        while (true)
        {
            GetComponent<SpriteRenderer>().flipX = !flipState;
            yield return new WaitForSeconds(1f);
            GetComponent<SpriteRenderer>().flipX = !flipState;
        }

    }
\$\endgroup\$
1
\$\begingroup\$

You're setting flipX to true and then you wait one second then set it to true again. I don't see how it's supposed to be flipping it. Also, even if you did flip it, it would be flipped once at first and then flipped twice at every other iteration after that, because your loop goes like this:

flip -> wait -> flip -> flip -> wait -> flip -> flip -> ... 

Something like this should work:

while (true)
    {
        GetComponent<SpriteRenderer>().flipX = flipState;
        flipstate = !flipstate;
        yield return new WaitForSeconds(1f);
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.