简体   繁体   中英

error with code for unity 2d flip a enemy sprite (IA) code?

i want to do a enemy IA chasing my character, i can do that, but when i want to code a script for the enemy sprite flip (Its a cube with 2 eyes) with a simple flip technique, the script doesnt work.

this is a little part of my code because a lot of then is for IA movement.

plz help

if (facingRight == false && moveInput > 0)
    {
        Flip();
    }
    else if (facingRight == true && moveInput < 0)
    {
        Flip();
    }
}

private void Flip()
{
    facingRight = !facingRight;
    transform.Rotate(0f, 180f, 0f);
}

Rotating your sprite is going to "flip" it, its going to well... rotate it.

The old fashioned way to flip a sprite was to set a negative scale, but that was a little counter intuitive so Unity helped us out by Adding flipX . I have updated your code below.

if (facingRight == false && moveInput > 0)
    {
        Flip();
    }
    else if (facingRight == true && moveInput < 0)
    {
        Flip();
    }
}

private void Flip()
{
    facingRight = !facingRight;
    GetComponet<SpriteRenderer>().flipX = facingRight;
}

Best of luck! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM