So I have these small panels (0.1 scale, not sure if I should be doing that, or just making the map bigger) I have a larger box collider on the panels, and they are a rigidbody. My code looks like this (Layer 8 is my flashlight, which I don’t want the panel to care about, otherwise it should turn left before it hits the object, which it isn’t doing)

void Start()
{
    ray = new Ray(transform.position, transform.forward);
}

void Update()
{
LayerMask layerMask = 1 << 8;

        if (Physics.Raycast(ray,out hit,50f,layerMask))
        {
           
           transform.Rotate(Vector3.up, -90 * 5 * Time.smoothDeltaTime);
        }
        else
        {
            if (UpDown)
                transform.Translate(0, 0, 2 * Time.deltaTime, Space.Self);
            else
                transform.Translate(0, 0, 2 * Time.deltaTime, Space.Self);
               
        }
}

EDIT:
When I use this code, I do hit the wall (though I spin in circles as walls are all around) whenever I add a distance, the plane just walks right up to the wall.

if (Physics.Raycast(ray,out hit))
            {
                Debug.Log(hit.distance.ToString() + " " + hit.collider.gameObject.name);
               transform.Rotate(Vector3.up, -90 * 5 * Time.smoothDeltaTime);
            }
            else
            {
                if (UpDown)
                    transform.Translate(0, 0, 2 * Time.deltaTime, Space.Self);
                else
                    transform.Translate(0, 0, 2 * Time.deltaTime, Space.Self);
                   
            }

The Layers page on the Unity Documentation shows how to do this.

You did create a layer mask but that layer mask is set to hit only layer 8. You’ll want to invert it before you use it.

layerMask = ~layerMask;

I’m not sure if you have realized that: You set your Ray in Start so it will never change it’s position or direction. I can see that you move and rotate your object in the script but your ray will ALWAYS stay at the initial position. It doesn’t move along with the object. I guess you want to reinit the ray in Update right before your Raycast.

Btw: The layerMask parameter of the Raycast function is just of type int. The LayerMask type has been invented to easily create a layermask value and to display it in the inspector. When you “create” your mask on your own by left-shifting (1 << 8) you don’t want to use the LayerMask type at all. Just use an int.

If you want to raycast against everything but layer 8 the value you need would be ~(1<<8)

I guess you also don’t want to raycast against the ignoreRaycast layer so you should setup your mask like this:

int myLayerMask = Physics.kDefaultRaycastLayers & (~(1<<8));
// or
int myLayerMask = ~((1<<2) | (1<<8));