Field of vision code not working, help!

Hi. I'm trying to implement this code but I can't get it to work for some reason.

I'm learning 3d modelling and have no experience of coding, so I'm really in the dark as to what to do. I'm trying to get the enemy to look at the character if the code conditions are true, and have added the code

transform.LookAt(target);

but it doesn't work.

It does work if I just use the above code on its own in an update function, but I only want the sentry to see the player when the conditions are met.

I've tagged the player object as Pill, dragged the Player object onto the target parameter in the script option so it knows what to look at, and dragged the script onto the enemy sentry. Please help, as I'm pulling my hair out and I'm sure it's something blindingly simple. If you could tell me exactly what to do it'b be brilliant - I'm still just finding my way around Unity at the moment;

var playerObject : GameObject; // the player var fieldOfViewRange : float; // in degrees (I use 68, this gives the enemy a vision of 136 degrees) var minPlayerDetectDistance : float; // the distance the player can come behind the enemy without being detected var rayRange : float; // distance the enemy can "see" in front of him private var rayDirection = Vector3.zero; var target : Transform;

function CanSeePlayer() : boolean 

{ var hit : RaycastHit; rayDirection = playerObject.transform.position - transform.position; var distanceToPlayer = Vector3.Distance(transform.position, playerObject.transform.position);

if(Physics.Raycast (transform.position, rayDirection, hit)){ // If the player is very close behind the enemy and not in view the enemy will detect the player
    if((hit.transform.tag == "Pill") && 

(distanceToPlayer <= minPlayerDetectDistance)){ //Debug.Log("Caught player sneaking up behind!"); transform.LookAt(target); return true;

` }
}

if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange){ // Detect if player is within the field of view
if (Physics.Raycast (transform.position, rayDirection, hit, rayRange)) {

    if (hit.transform.tag == "Pill") {
        //Debug.Log("Can see player");
        transform.LookAt(target);
        return true;

    }else{
        //Debug.Log("Can not see player");
        return false;
    }
}

}
}

function OnDrawGizmosSelected ()
{
`

// Draws a line in front of the player and one behind this is used to visually illustrate the detection ranges in front and behind the enemy Gizmos.color = Color.magenta; // the color used to detect the player in front Gizmos.DrawRay (transform.position, transform.forward * rayRange); Gizmos.color = Color.yellow; // the color used to detect the player from behind Gizmos.DrawRay (transform.position, transform.forward * -minPlayerDetectDistance); }

P.S. sorry for the code coming out all weird, the site changed the layout and cut off some bits of code so I had to rearrange it to make it visible.

How often are you calling this function? The function seems to do what it should, but I don't see where you actually call it. Try putting it into an update cycle...I'd recommend trying something like this:

function Update()
{
   if(CanSeePlayer())
   {
      Debug.Log("Can See Player!");
      transform.LookAt(target);
   }
   else
   {
      Debug.Log("Cannot see player.");
   }
}

function CanSeePlayer() : boolean 
{
    var hit : RaycastHit; 
rayDirection = playerObject.transform.position - transform.position; 
var distanceToPlayer = Vector3.Distance(transform.position, playerObject.transform.position);

if(Physics.Raycast (transform.position, rayDirection, hit))
{ // If the player is very close behind the enemy and not in view the enemy will detect the player
    if((hit.transform.tag == "Pill") && (distanceToPlayer <= minPlayerDetectDistance))
    {
        return true;
    }
}

if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange)
{ // Detect if player is within the field of view
    if (Physics.Raycast (transform.position, rayDirection, hit, rayRange)) 
    {
        if (hit.transform.tag == "Pill") 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
}