Casting multiple rays from one object into one function

I’m trying to cast multiple rays from an object and I’m having trouble with the code.
I can easily make it work but the problem is that the code is just way too long I think, and I don’t know if ti can be done with just a simple function.

Here’s my code

        RaycastHit hit;
        Ray rayDown1 = new Ray(transform.position, Vector3.down);
        Ray rayDown2 = new Ray(transform.position + new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 2), 0, 0), Vector3.down);
        Debug.DrawRay(transform.position + new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, 0), Vector3.down, Color.blue);
        Ray rayDown3 = new Ray(transform.position - new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, 0), Vector3.down);
        Ray rayDown4 = new Ray(transform.position - new Vector3(0, 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
        Ray rayDown5 = new Ray(transform.position + new Vector3(0, 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
        Ray rayDown6 = new Ray(transform.position + new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
        Ray rayDown7 = new Ray(transform.position - new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
        Ray rayDown8 = new Ray(transform.position + new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, -(this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
        Ray rayDown9 = new Ray(transform.position - new Vector3(-(this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);

            if (Physics.Raycast(rayDown1, out hit))
            {
                if (hit.collider.tag == "Boost")
                {
                    PlateauScript script = hit.collider.GetComponent<PlateauScript>();
                    if (!script.wasUsed) GameHandler.Instance.PushForce = script.BoostAmount;

                }
                if (hit.collider.tag == "Checkpoint" && GameHandler.Instance.MovementSpeed != 0)
                {
                    GameHandler.Instance.LastCheckpoint = hit.collider.gameObject;
                    GameHandler.Instance.CheckpointSpeed = GameHandler.Instance.MovementSpeed;
                }
            }

I’m trying to get all these rays to work with the if statement below there but I can’t find an easy way to do this without using a lot of code and a lot of if statements.

RayTest(_rayDown1);
RayTest(_rayDown2);
RayTest(_rayDown3);
RayTest(_rayDown4);
etc…

void RayTest(Ray _ray)
{
    RaycastHit hit;
    if(Physics.Raycast(_ray, out hit))
    {
        // your code
    }
}

If each ray falls under that same conditions just pass each created ray into this function.

It looks like you just want to cast 9 rays in a 3x3 pattern around your gameobject. Just use two for loops:

// might by a class variable
bool noGround = false;

Vector3 pos = transform.position;
Vector3 extents = gameObject.GetComponent<Renderer>().bounds.extents;
// before our check set it to true
noGround = true;
for (int x = -1; x <=1; x++)
{
    for (int y = -1; y <=1; y++)
    {
        Ray ray = new Ray(pos +new Vector3(x*extents.x, 0f, y*extents.y), Vector3.down);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            // if we hit anything with any ray we can set "noGround" to false.
            // only if none of the rays hit something noGround will be true
            noGround = false;
            if (hit.collider.tag == "Boost")
            {
                PlateauScript script = hit.collider.GetComponent<PlateauScript>();
                if (!script.wasUsed) GameHandler.Instance.PushForce = script.BoostAmount;
            }
            if (hit.collider.tag == "Checkpoint" && GameHandler.Instance.MovementSpeed != 0)
            {
                GameHandler.Instance.LastCheckpoint = hit.collider.gameObject;
                GameHandler.Instance.CheckpointSpeed = GameHandler.Instance.MovementSpeed;
            }
        }
    }
}

edit
I have added the “noGround” variable that will be true when all rays go into the void and don’t detect anything. Note: this is not a good way to check if a player has fallen out of the level. If you for example jump over a gap, it’s possible that during the jump there’s no ground below the character.

It’s safer to either add an “height check” and specify some lower threshold and when the player goes below that you know he’s fallen out, or use trigger boxes that cover the void areas and when your player hits those he’s fallen out.