AI not attacking player

I’m using the FPS AI and I’ve put a bunch of debug lines in it to try to figure out why its not doing anything.

function Patrol () {

	Debug.Log("In Patrol");
	var curWayPoint = AutoWayPoint.FindClosest(transform.position);
	while (true) {
		var waypointPosition = curWayPoint.transform.position;
		// Are we close to a waypoint? -> pick the next one!
		if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
		{
			curWayPoint = PickNextWaypoint (curWayPoint);
			Debug.Log("Picking Waypoint");
		}	
			
		// Attack the player and wait until
		// - player is killed
		// - player is out of sight		
		if (CanSeeTarget ())
		{
			Debug.Log("About to Attack Player");
			yield StartCoroutine("AttackPlayer");
			
		}
		
		// Move towards our target
		MoveTowards(waypointPosition);
		Debug.Log("Moving Towards Player");
		
		yield;
	}
}

function CanSeeTarget () : boolean {

	Debug.Log("In Can See Target");
	if (Vector3.Distance(transform.position, target.position) > attackRange)
	{
		Debug.Log("Out of Attack Range");
		return false;
	}	
		
	var hit : RaycastHit;
	if (Physics.Linecast (transform.position, target.position, hit))
	{
		Debug.Log("Found Target");
		return hit.transform == target;
	}

	return false;
}

I’m getting a “Found Target” return but not “About to Attack Player”

Does anyone know why this unaltered AI.js code from the FPS example isn’t working in this instance?

I removed the huge chunks of code that probably scared everyone away from this post and bump it.

My first guess would be that CanSeeTarget() never returns true :stuck_out_tongue:

Yes, in that case “return hit.transform == target;” returns false. Did you set the target (if you actually need to).

Try debugging the hit.transform. Check the name for example.