Raycast collision detection for enemy

i made a collision detection for a enemy using some tutorials. the enemy chases the player around and i want it to collide with the player and walls when the raycast hit the colliders. i cant tell if my code is wrong cause it doesnt seem to collide with the walls or the player who is also moving.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AIfollow : MonoBehaviour
{
    //variables
    public GameObject player;
    public float speed;
    private float distance;
    private BoxCollider2D enemycollider;

    void Start()
    {
        //gets enemy box collider
        enemycollider = GetComponent<BoxCollider2D>();
    }

    void Update()
    {   
        //measures distance between enemy and player
        distance = Vector2.Distance(transform.position, player.transform.position);
       
        //gets the direction for the enemy to go
        Vector2 direction = player.transform.position - transform.position;
        
        //makes direction have a magnitude of one and below
        direction.Normalize();
        
        //gets the x and y of the enemy direction
        float XEnemy = direction.x;
        float YEnemy = direction.y;

        //sends out a bunch of rays to check for collision
        RaycastHit2D castResult;
        castResult = Physics2D.BoxCast(transform.position, enemycollider.size, 0, new Vector2(XEnemy, 0), Mathf.Abs(XEnemy * Time.fixedDeltaTime * speed), LayerMask.GetMask("wall", "player"));

        //sets movement in x axis to zero if collide
        if (castResult.collider)
        {
            direction.x = 0;
        }

        //sends raycast
        castResult = Physics2D.BoxCast(transform.position, enemycollider.size, 0, new Vector2(0, YEnemy), Mathf.Abs(YEnemy * Time.fixedDeltaTime * speed), LayerMask.GetMask("wall", "player"));

        //sets movement in y axis to zero if collide
        if (castResult.collider)
        {
            direction.y = 0;
        }

        //finds angle between two points
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        //moves the enemy to the player position
        transform.position = Vector2.MoveTowards(this.transform.position, player.transform.position, speed * Time.deltaTime);
        //makes enemy face player
        transform.rotation = Quaternion.Euler(Vector3.forward * angle);
    }
}