I need an explanation about raycast and collisions

Hey Guys!

So, I’m trying to script a simple AI routine for the enemy bots for my game.

Thanks to you all, I’ve managed to make a patrol using waypoints, now, I wanto to make this.

If the player enter in the enemy view, the enemy will pursuit and attack (melee), and if the enemy can’t see the player anymore, backs to the patrol.

I believe that, the best way to do this, is using the raycast and colliders, so, can you guys tell me a little more about colliders and raycasts? I mean, the question is:

How can my enemies “see the walls”?

How can I use the colliders to make my enemies check if is a wall or the hero?

How can I use the raycast to make a viewpoint for my enemies?

And, how can I make my hero collide to the enemy’s view, and NOT see behind the walls?

I hope don’t act confuse (my English doesn’t help, though), but I appreciate your kindness, anyway

The most used way to “see” a target without that annoying X-ray effect is to use Physics.Linecast(enemy.position, target.position), and check if the object hit is the target - if it’s not, there’s something in between:

function CanSeeTarget(target: Transform): boolean {
  var hit: RaycastHit;
  if (Physics.Linecast(transform.position, target.position, hit)
      && hit.transform == target){
    return true;
  }
  return false;
}

You can also add more restrictions, like limited view range and angle:

var range: float = 100; // viewing range
var viewAngle: float = 15; // viewing angle

function CanSeeTarget(target: Transform): boolean {
  var hit: RaycastHit;
  if (Physics.Linecast(transform.position, target.position, hit, range)
      && hit.transform == target
      && Vector3.Angle(target.position-transform.position, transform.forward) < viewAngle){
    return true;
  } else {
    return false;
  }
}