How do I allow the player to push multiple objects in a grid based 2D game?

This is the script that detects nearby objects that can be pushed & the wall:

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

public class PlayerNearbyObjectDetection : MonoBehaviour
{
    public bool pushableObjectAtLeft;
    public bool pushableObjectAtRight;
    public bool pushableObjectAtTop;
    public bool pushableObjectAtBottom;
    public float pushableObjectCountLeft;
    public float pushableObjectCountRight;
    public float pushableObjectCountTop;
    public float pushableObjectCountBottom; 
    public Transform leftRayCastObject;
    public Transform rightRayCastObject;
    public Transform topRayCastObject;
    public Transform bottomRayCastObject;
    public LayerMask obstacle; 
    void Update() 
    {
        RaycastHit2D leftRayCast = Physics2D.Raycast(transform.position, Vector2.left, 1f); 
        RaycastHit2D rightRayCast = Physics2D.Raycast(transform.position, Vector2.right, 1f);
        RaycastHit2D topRayCast = Physics2D.Raycast(transform.position, Vector2.up, 1f); 
        RaycastHit2D bottomRayCast = Physics2D.Raycast(transform.position, Vector2.down, 1f);
        RaycastHit2D[] leftExtendedRayCast = Physics2D.RaycastAll(transform.position, Vector2.left, 2f); 
        RaycastHit2D[] rightExtendedRayCast = Physics2D.RaycastAll(transform.position, Vector2.right, 2f);
        RaycastHit2D[] topExtendedRayCast = Physics2D.RaycastAll(transform.position, Vector2.up, 2f);
        RaycastHit2D[] bottomExtendedRayCast = Physics2D.RaycastAll(transform.position, Vector2.down, 2f);
        if (leftRayCast.collider != null)
        {
            leftRayCastObject = leftRayCast.collider.transform; 
            if (leftRayCast.collider.transform.gameObject != null && leftRayCast.collider.transform.gameObject.CompareTag("Pushable Object"))
            {
                pushableObjectAtLeft = true;
                pushableObjectCountLeft += 1; 
                if (!leftExtendedRayCast[leftExtendedRayCast.Length - 1].collider.transform.gameObject.CompareTag("Pushable Object")) 
                {
                    gameObject.GetComponent<PlayerMovement>().canMoveLeft = false;
                }
            }
        }
        else
        {
            leftRayCastObject = null;
            pushableObjectAtLeft = false;
            gameObject.GetComponent<PlayerMovement>().canMoveLeft = true;
            pushableObjectCountLeft = 0; 
        }
        if (rightRayCast.collider != null) 
        {
            rightRayCastObject = rightRayCast.collider.transform;
            if (rightRayCast.collider.transform.gameObject != null && rightRayCast.collider.transform.gameObject.CompareTag("Pushable Object"))
            {
                pushableObjectAtRight = true;
                if (!rightExtendedRayCast[rightExtendedRayCast.Length - 1].transform.gameObject.CompareTag("Pushable Object"))
                {
                    gameObject.GetComponent<PlayerMovement>().canMoveRight = false;
                }
            }
        }
        else
        {
            rightRayCastObject = null; 
            pushableObjectAtRight = false;
            gameObject.GetComponent<PlayerMovement>().canMoveRight = true; 
        }
        if (topRayCast.collider != null)
        {
            topRayCastObject = topRayCast.collider.transform;
            if (topRayCast.collider.transform.gameObject != null && topRayCast.collider.transform.gameObject.CompareTag("Pushable Object"))
            {
                pushableObjectAtTop= true;
                if (!topExtendedRayCast[topExtendedRayCast.Length - 1].transform.gameObject.CompareTag("Pushable Object"))
                {
                    gameObject.GetComponent<PlayerMovement>().canMoveUp = false;
                }
            }
        }
        else
        {
            topRayCastObject = null;
            pushableObjectAtTop = false;
            gameObject.GetComponent<PlayerMovement>().canMoveUp = true; 
        }
        if (bottomRayCast.collider != null)
        {
            bottomRayCastObject = bottomRayCast.collider.transform;
            if (bottomRayCast.collider.transform.gameObject != null && bottomRayCast.collider.transform.gameObject.CompareTag("Pushable Object"))
            {
                pushableObjectAtBottom = true;
                if (!bottomExtendedRayCast[bottomExtendedRayCast.Length - 1].transform.gameObject.CompareTag("Pushable Object"))
                {
                    gameObject.GetComponent<PlayerMovement>().canMoveDown = false;
                }
            }
        } 
        else
        {
            bottomRayCastObject = null; 
            pushableObjectAtBottom = false;
            gameObject.GetComponent<PlayerMovement>().canMoveDown = true; 
        }
    }
}

This is the script that the pushable object uses to move:

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

public class PushableObject : MonoBehaviour
{
    public GameObject player;
    public Transform movePoint;
    public LayerMask obstacle;
    public LayerMask pushableObject;
    void Start()
    {
        movePoint.parent = null; 
    }
    void Update()
    {
        if (!Physics2D.OverlapCircle(movePoint.position, 0.2f, obstacle))
        {
            transform.position = Vector3.MoveTowards(transform.position, movePoint.position, player.GetComponent<PlayerMovement>().movementSpeed * Time.deltaTime);
        }
    }
}

Now these two scripts can move one object, but cannot be utilized for pushing multiple objects in a row. Is there a way I can modify them to do that?

The process needs to be:

if the player pushes on a block:

  • start at that block and iterate each block in a row in the direction of push
  • keep going until:
    -----> you find an immovable block
    -----> you find an open space

When you’re done, how far you iterated tells you how many blocks need to be pushed.

That will work for any number of blocks in a row.