Why is my enemy pathfinding not working?

I am making a 2d platformer, and i am trying to add enemies. i am trying to make the enemy move back and forth on a platform, flipping its direction when it gets to the edge or a wall. i am using empty objects and unity’s overlap circle feature to detect if the enemy is close to a wall or edge. however, the enemy is constantly flipping back and forth. Am i doing something wrong, and if so how do i fix it? here is the enemy pathfinding code:

private void EnemyMove()
{
if (!IsGrounded() || IsWalled())
{
Flip();
}
else
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
}

public bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);

}

public bool IsWalled()
{
return Physics2D.OverlapCircle(wallCheck.position, 0.2f, groundLayer);
}

private void Flip()
{
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
horizontal = horizontal * -1;
}

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.