AI Raycast problem

I’m drafting a system where an AI sends out a raycast to a target to check if there is something in front of it and i’m not sure how to go about it. I have a boolean variable called canSeeTarget which if it’s set to true the AI draws a green line or if it equals false it draws a red line.

I have another variable which called target which is a transform. So if canSeeTarget equals true, the AI will move towards the target. This may sound a bit unclear so I’ll write an example of what I’m doing in javascript

var target : Transform;
var canSeeTarget : boolean = false;
var targetDistance : int;
var thisAI : GameObject;

function Awake()
{
   canSeeTarget = false;
}

function Update()
{
   targetDistance = Vector3.Distance(target.position,    transform.position);

      if(canSeeTarget == true)
    {
      Debug.DrawLine(transform.position, target.position, Color.green);
      thisAI.animation.CrossFade("WalkAim");
    }
   
   if(canSeeTarget == false)
    {
      Debug.DrawLine(transform.position, target.position, Color.red);
      thisAI.animation.CrossFade("Idle");
    }
}

This script only draws a line to the target and changes colour when canSeeTarget is changed. What I want to do is send a raycast to check if the target can actually be seen instead of seeing through objects and if it has been seen, I can tell it what to do from there. Any suggestions or help would be greatly appreciated

Thanks, Jordz

canSeeTarget = false;
var hit : RaycastHit;
if (Physics.Linecast(transform.position, target.position, hit) {
if (hit.collider.name == “TargtsName”) {
canSeeTarget = true;
}
}

Note this only tests to see if the pivot position of the AI can see the pivot position of the target. You may want to raycast from an empty game object at the eyes of the AI. And you may want to test the corners of the bounding box of the target rather than just the pivot.