Can See Target Not Working

Hello, everybody. I’ve got a problem with my AI Script. Although I want it to check if there is something in the way of the player and thereby give up pursuit/shooting at him, CannotSeeTarget() : boolean never returns false. I was hoping that there would be a way to fix this. All help would be appreciated!!! :slight_smile:

Here is my Code:

enum s
{
	Idle,
	Pursuit,
	Shooting
}	
var Player : Transform;
var walkSpeed : float;
var runSpeed : float;
var obstacleAvoidanceLevel : float;
var patrolChangesInterval : float;
var shootRange : float;
var noticeRange : float;
var showGizmosInEditor : boolean;
var avoidEdges : boolean;
var gun : AIGun;

var Controller : CharacterController;

@HideInInspector
var state : s;

private var lastChange : float;
private var moveSpeed : float;
function Awake()
{
	patrolChangesInterval += Random.Range(-1.5, 1.5);
	moveSpeed = walkSpeed;
}	
function Update()
{
	Debug.Log(CannotSeeTarget());
	Controller.Move(-Vector3.up * Time.deltaTime * 10);
	Controller.Move(transform.forward * moveSpeed*Time.deltaTime);
	if(Time.time > lastChange)
	{
		lastChange = Time.time + patrolChangesInterval;
		ChangeDirection();
	}
	
	var hit : RaycastHit;
	if(Physics.Raycast(transform.position, transform.forward, hit, obstacleAvoidanceLevel))
	{
		if(hit.collider.tag != "Player")
		{
			ChangeDirection();
		}
	}
	var dist : float;
	dist = Vector3.Distance(Player.transform.position, transform.position);
	if(dist < noticeRange && dist > shootRange && !CannotSeeTarget())
	{
		state = s.Pursuit;
		moveSpeed = runSpeed;
	} else if(dist < shootRange && !CannotSeeTarget())
	{
		state = s.Shooting;
		moveSpeed = 0;
	} else {
		state = s.Idle;
		moveSpeed = walkSpeed;
	}
	if(state != s.Idle)
	{
		transform.LookAt(Player.transform);
		if(state == s.Shooting)
		{
			gun.Shoot(transform.forward, transform.rotation);
		}
	}
		
	
}
function ChangeDirection ()
{
	transform.Rotate(Vector3(0, Random.Range(90, 270), 0));
}
function CannotSeeTarget() : boolean
{
	var hit : RaycastHit;
	return Physics.Linecast(transform.position, Player.position, hit);
	if(hit != null)
		Debug.Log(hit.collider.name);
}
function OnDrawGizmosSelected () 
{
	if(showGizmosInEditor)
	{
		Gizmos.color = Color.yellow;
		Gizmos.DrawWireSphere(transform.position, noticeRange);
		Gizmos.color = Color.red;
		Gizmos.DrawWireSphere(transform.position, shootRange);
	}
}

If CannotSeeTarget never returns false, it is because there is something in the way of your linecast. This could be because it is linecasting from the pivot point of your enemy to the players pivot point and they are not where you thought they’d be. No matter the reason you need to find out what and where it goes wrong, so save the point where it hits and Gizmos.DrawLine from the 2 points should reveal something.

Hope this helps :slight_smile:

var HitPosition : Vector3;
function CannotSeeTarget() : boolean
{
    var hit : RaycastHit;
    if(Physics.Linecast(transform.position, Player.position, hit))
    {
        HitPosition = hit.point;
        return true;
    }
    else
    {
        return false;
    }
    if(hit != null)
        Debug.Log(hit.collider.name);
}
function OnDrawGizmosSelected () 
{
    if(showGizmosInEditor)
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position, noticeRange);
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, shootRange);
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(transform.position, Player.position);
        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(HitPosition , 1);
    }
}