Аi does not change the direction of movement in a collision

I wanted to make an AI that would walk from wall to wall.

But he hits the wall and starts shaking. Observation: if the AI hits the AI, then everything works (changes the direction of movement)

My code

void Move()
    Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position + transform.up * 0.1f + transform.right * dir.x * 0.8f, 0.1f);
    if (colliders.Length > 1) dir *= -1f;
    transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, Time.deltaTime);

I apologize for my English

using UnityEngine;

public class WallToWallAI : MonoBehaviour
    public float movementSpeed = 3f;
    public float wallDetectionDistance = 1f;

    private bool isFacingRight = true;
    private Rigidbody2D rb;

    private void Awake()
        rb = GetComponent<Rigidbody2D>();

    private void Update()
        // Detect walls
        RaycastHit2D wallHit = Physics2D.Raycast(transform.position, transform.right, wallDetectionDistance);
        // Check if wall is detected
        if (wallHit.collider != null)
            // Change direction if a wall is detected
            isFacingRight = !isFacingRight;

        // Move towards the current direction
        Vector2 movement = isFacingRight ? Vector2.right : Vector2.left;
        rb.velocity = movement * movementSpeed;

This happens because the AI hits the wall, so the conditional returns true, but after turning, it is still touching the wall, so it turns again. Program it to move forward until it is not touching the wall after it rotates. The AI work when they hit each other because they both move, so they are no longer touching.