How to make Enemy Sprite Flip?

37 views Asked by At

I got an Enemy and I wanna flip the Sprite when his X - Value is increasing and the Opposite, in my PlayerMovement Skript this kind of Method worked but now it doesn't anymore.

 private void UpdateDirection()

 {
     if (transform.position.x < -.1f) 
     {
         spr.flipX = true; 
     }
     else
     {
         spr.flipX = false; 
     }
 }
1

There are 1 answers

0
AudioBubble On

Does it need to check if its moved, cant you just check the rigidbodys velocity like this:

private Rigidbody2D m_Rigidbody;

void Awake() {
    m_Rigidbody = GetComponent<Rigidbody>();
}
void Update() {
    DirectionCheck();
}

private void DirectionCheck() {
    if (m_Rigidbody.velocity.x > 0 && transform.localScale.x < 0)
        Flip();
    else if (m_Rigidbody.velocity.x < 0 && transform.localScale.x > 0)
        Flip();
}
private void Flip() {
    Vector2 newScale = transform.localScale;
    newScale.x *= -1;
    transform.localScale = newScale;
}