# FAIL [CLOSED] Why doesn this never return PlayerRay as true?

Why does this never return PlayerRay as true? I am guessing this is the problem since the enemy never follows the player? (It may be a different problem though) Thanks :slight_smile:

var Player : GameObject;
var PlayerDirection;

var BulletDamage;
private var HeadShotDamage;
var MaxHeadShotDamage : float = 500;
var MinHeadShotDamage : float = 400;

var Ai : GameObject;
var Health : int = 100;
var HealthText : TextMesh;

var MoveSpeed : float = 2.5;

var GunShot : GameObject;
var GunDamage : int;
var GunTimer : float = 0.3;

var MinPlayerDistance : int = 5;
var MaxPlayerDistance : int = 30;

var PlayerClose : boolean;
var PlayerInFov : boolean;
var PlayerRay : boolean;



function Start()
{
	PlayerInFov = false;
	PlayerRay = false;
	
	
}

function Update()
{
	Player = GameObject.FindWithTag("Player");
	PlayerDirection = Player.transform.position - Ai.transform.position;
	
	BulletDamage = Player.GetComponent("Weapon Fire").Damage;
	HeadShotDamage = Random.Range(MinHeadShotDamage, MaxHeadShotDamage);
	GunDamage = Random.Range(10, 15);
	
	HealthText.GetComponent(TextMesh).text = Health.ToString();
	
	var PlayerDir = Player.transform.position - transform.position;
	Forward = transform.forward;
	Angle = Vector3.Angle(PlayerDir, Forward);
	
	var PlayerShot : RaycastHit;
	
	if (Angle < 60)
	{
		PlayerInFov = true;
	}
	else
	{
		PlayerInFov = false;
	}
	
	if(Vector3.Distance(transform.position, Player.transform.position) <= MaxPlayerDistance)
	{
		PlayerClose = true;
	}
	else
	{
		PlayerClose = false;
	}
	
	if(Physics.Raycast(Ai.transform.position, PlayerDirection, PlayerShot))
	{
		if(PlayerShot.transform.tag == "Player")
		{
			PlayerRay = true;
		}
		else
		{
			PlayerRay = false;
		}
	}
	
	if(PlayerClose && PlayerInFov && PlayerRay)
	{
		transform.LookAt(Vector3(Player.transform.position.x, transform.position.y, Player.transform.position.z));
		transform.position += Forward * MoveSpeed * Time.deltaTime;
		
	}
	
	if(Health < 0)
	{
		Health = 0;
	}
	
	if(Health == 0)
	{
		Destroy(gameObject);
	}
}

function OnTriggerEnter(Bullet : Collider)
{
	if(Bullet.gameObject.tag == "Bullet")
	{
		Health -= BulletDamage;
		Destroy(Bullet);
	}
}

Probably because the Ray isn’t pointing at the player, or the player object doesn’t have it’s tag set correctly. Try the following:

aiPosition : Vector3 = Ai.transform.position;
if(Physics.Raycast(aiPosition, aiPosition - Player.transform.position, PlayerShot))
    PlayerRay = PlayerShot.transform.tag.CompareTo("Player");