How to detect if floor exists using Raycast

I’m trying to make an NPC that will switch directions at the end of a platform, so I decided to try and detect the platform using Raycast. However, when the ray reaches an area where there is no floor, it jolts back and forth erratically. I’m not sure what causes this.

void Movement()
    {
        float speed = idleSpeed * Time.deltaTime;

        Vector2 v = GetComponent<Rigidbody2D>().velocity;
        v.x = speed;
        

        RaycastHit2D[] floorDetectRight;
        RaycastHit2D[] floorDetectLeft;

        Vector2 leftRay = new Vector2(GetComponent<Transform>().position.x - enemy.bounds.size.x, GetComponent<Transform>().position.y);
        floorDetectLeft = Physics2D.RaycastAll(leftRay, -Vector2.up, enemy.bounds.size.y);

        Vector2 rightRay = new Vector2(GetComponent<Transform>().position.x + enemy.bounds.size.x, GetComponent<Transform>().position.y);
        floorDetectRight = Physics2D.RaycastAll(rightRay, -Vector2.up, enemy.bounds.size.y);


        if (GetComponent<Rigidbody2D>().velocity.x > 0 && floorDetectRight.Length < 1)
        {
            v.x = -speed;
        }

        if (GetComponent<Rigidbody2D>().velocity.x < 0 && floorDetectLeft.Length < 1)
        {
            v.x = speed;
        }
        GetComponent<Rigidbody2D>().velocity = v;
    }

Another problem with this code is that it won’t trigger if something other than the floor is there. It will only trigger if nothing is there. I did try if (GetComponent<Rigidbody2D>().velocity.x < 0 && floorDetectLeft.collider.tag != "floor") but when I had it like that, it didn’t trigger at all…

If you can see what’s wrong with the code, please let me know. As far as I’m aware, this should work. Thanks in advance!

Managed to fix it by adding a detect floor function and shuffling some things around.

    void Movement()
    {
        
        Vector2 v = GetComponent<Rigidbody2D>().velocity;

        if (DetectFloor())
        {
            goRight = !goRight;
            StartCoroutine(WaitSecond());
        }
        
        if(goRight)
        {
            v.x = idleSpeed * Time.deltaTime;
            GetComponent<Rigidbody2D>().velocity = v;   
        }
        else 
        {
            v.x = -idleSpeed * Time.deltaTime;
            GetComponent<Rigidbody2D>().velocity = v;
        }



    }

    bool DetectFloor()
    {
        RaycastHit2D[] floorDetectRight = new RaycastHit2D[1];
        RaycastHit2D[] floorDetectLeft = new RaycastHit2D[1];

        Vector2 leftRay = new Vector2(GetComponent<Transform>().position.x - enemy.bounds.size.x, GetComponent<Transform>().position.y);
        Physics2D.RaycastNonAlloc(leftRay, -Vector2.up, floorDetectLeft, enemy.bounds.size.y);

        Vector2 rightRay = new Vector2(GetComponent<Transform>().position.x + enemy.bounds.size.x, GetComponent<Transform>().position.y);
        Physics2D.RaycastNonAlloc(rightRay, -Vector2.up, floorDetectRight, enemy.bounds.size.y);

        if (floorDetectLeft[0].collider == null)
        {
            return true;
        }

        if (floorDetectRight[0].collider == null)
        {
            return true;
        }

        return false;
    }

    IEnumerator WaitSecond()
    {
        yield return new WaitForSeconds(1);
    }
}