Make Raycast ignore collider on boolean

Hey guys!
very simple question. how do i make that my raycast ignore some kinds of colliders and just pass through them?
I got tags for those colliders but i dont know how i can make that i can switch it on and off.

var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
var Raycastignore : boolean;
	
if(Physics.Raycast(transform.position, fwd , hit, rayLength))
{
  if(!Raycastignore)
  {
    if(hit.collider.gameobject.tag == "collider")
     {
      do stuff
     }
   else
   {
    if(hit.collider.gameobject.tag == "collider")
    {
     just ignore and pass through and then do stuff there.
    }
   }
}

On the forum I just found on making raycast ignore objects but I didnt foudn how to make it with a boolean. thanks for help!

Rather than use tags, you should use Layers. Put the objects you want to ignore in a different Layer (or Layers), then do something like this:

var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
var hitLayers : LayerMask;
var ignoreLayers: LayerMask;
private var _raycastLayers : LayerMask;
var RaycastIgnore: boolean;

if (RaycastIgnore) {
    var invertedHitLayers = ~hitLayers;
    _raycastLayers = ~(invertedHitLayers | ignoreLayers);
} else {
    _raycastLayers = hitLayers;
}
     
if(Physics.Raycast(transform.position, fwd , hit, rayLength, _raycastLayers))
{
    if(hit.collider.gameObject.CompareTag("collider"))
    {
         if (RaycastIgnore) {
           // do stuff
         } else {
           // do other stuff
         }
    }
}

hitLayers are all the layers you normally want the raycast to hit. ignoreLayers are the layers to ignore when RaycastIgnore = true. These will appear as a dropdown in the Inspector where you can select the layers required.

We do the actual raycast using the _raycastLayers variable - toggling RaycastIgnore will change this from ignoring those layers to hitting those layers.

Note the use of the tilde (~) - this is a bitwise operation that inverts a LayerMask so that the ray will not hit those layers. The pipe | symbol is a bitwise OR, which effectively combines two layer masks. The lines: var invertedHitLayers = ~hitLayers; _raycastLayers = ~(~hitLayers | ignoreLayers); inverts hitLayers, adds ignoreLayers, then inverts everything - if my logic is correct, this will remove ignoreLayers from hitLayers.

A simpler alternative if you want to avoid the bitwise operations could be to just define one LayerMask for when RaycastIgnore = true and another for when RaycastIgnore = false.

Related Unity docs: “Casting Rays Selectively”.