I am building a simple line of sight script in unity using javascript and on a flat plane it works just fine. However on terrain i get some issues mainly the object can’t seem to see the player. Is there an adjustment i can make for this?
right now this is the code i have for detecting the player:
function CanSeePlayer() : boolean
{
var hit : RaycastHit;
var rayDirection = Target.transform.position - transform.position;
// If the Target is close to this object and is in front of it, then return true
if((Vector3.Angle(rayDirection, transform.forward)) < (fieldOfViewRange*2) && (Vector3.Distance(transform.position, Target.transform.position) <= attackRange))
{
inAttackRange = true;
return true;
}
else
{
inAttackRange = false;
}
if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange)
{ // Detect if player is within the field of view
if (Physics.Raycast (transform.position, rayDirection, hit, SightRange))
{
if (hit.collider.gameObject == Target)
{
return true;
}
else
{
return false;
}
}
}
}
any help would be great.
thanks.