Linecast not working with FPS-prefab?

Im having a curious problem when using Linecast on an enemy (NPC) to detect the Player which is an instance of the "First Person Controls"-prefab (or FPC from now on) in the iPhone Standard Assets.

It will detect the FPC if it is within range and the FPC-object is moved manually within the scene-editor using the mouse. This also changes in real time the values of the FPC's Transform->Position in the Inspector.

But when using the actual controls of the iPhone (via the remote) the FPC is completely ignored by Linecast. Also, the values in the Inspector does not change when moving (Transform->Position).

Ive noticed one curious thing: if the FPC is selected in the Scene-editor and moved by the iPhone via the remote, the center of the FPC will not follow the actual object completely, but stick to a middle point from where the FPC was spawned and where it acually is at any given moment. It's as if the FPC actually got bigger (thus displacing its center) rather than moving.

Another weird thing (which might be a clue for the insighful) is that Raycast does not display the same problems when detecting the FPC.

Anybody have an explanation and preferably also a solution to this weird behaviour?

</p>

<p>var target : Transform;</p>

<p>var smooth : float = 1.0;</p>

<p>function Update ()</p>

<p>{</p>


```
if( Physics.Linecast( transform.position, target.position ))

{   
    // TODO: Check distance
    // Doesnt work when moving player with iPod 
    if( Vector3.Distance( target.position, transform.position ) < 10.0 )

    {
    	// Rotate towards player
		var targetPoint = target.transform.position; 
        var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up); 
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0); 

    	// Move towards player  	
        var targetPosition = Vector3(target.position.x, transform.position.y, target.position.z); 
		transform.position = Vector3.Lerp( transform.position, targetPosition, Time.deltaTime * smooth );
    }
}

```


<p>}</p>

Have you tried adding debug? Assuming you have set up colliders correctly and the origin of the object with script isn't inside the other object's center the Physics.Linecast should always return true (making the script in current form quite useless I'd say).

What you probably want is to test if there's anything between the enemy and the player character. To check this, there are several ways, one is to have player in its own layer and test if the line from enemy to player hits anything in layers other than player. Another method is to look at the object the line/raycast is colliding with, if this is player there's nothing in between.

Sample how to do this:

var target : Transform; // set to player
var layerMask : LayerMask; // set to everything except player layer

function Update() { 
    if (!Physics.Linecast(transform.position, target.position , layerMask.value)) {
    	// player is in line of sight, do your thing here...
    }	
}