How to block a raycast

I'm using modified code from the FPS tutorial, to say when the player gets in range of an enemy which is patrolling around. However, I want the player to be able to hide in shadows and the enemy to walk past.

I tried creating boxes with invisible textures on them, and set them to trigger, but the raycast sees right through them. This is the code:

function CanSeeTarget () : boolean {
if (!isInFront)
    return false;

if (Vector3.Distance(transform.position, target.position) > attackRange)
    return false;

var hit : RaycastHit;
if (Physics.Linecast (transform.position, target.position, hit))
    return hit.transform == target;

return false;

}

I don't know how I can block the raycast, while still allowing the player to pass through the shadowed area. Thanks for any help.

Look at the Raycast documentation. There's a parameter that lets you set the Layer mask that the raycast will use. This makes it so the raycast can only "see" what's on the current layer.

Subsequently, everything with the built-in Ignore Raycast layer is automatically ignored by the raycaster.

What I did, sounds like what you did on your first try, but I made the box more like a hidden area. I created an large trigger encompassing the shadowy area and made a boolean like isVisible or something similar. When the player entered the box, I turned the boolean off and when he left I turned it on again. So then I just added one more conditional in the enemy script to check the bool.

The trigger went something like:

function OnTriggerEnter (col : Collider) {
     if(col.tag == "Player") {
         col.GetComponent(InvisibleController).isVisible = false;
     }
}

function OnTriggerExit (col : Collider) {
     if(col.tag == "Player") {
         col.GetComponent(InvisibleController).isVisible = true;
     }
}

then I added an extra check to the enemy AI script.

//There are some options as where to put the check.

if (Physics.Linecast (transform.position, target.position, hit)) {
     if(player.isVisible) { //player should be the script with the bool
         return hit.transform == target;
     }
}

return false;

}