I have code to check to see if an enemy can “see” the player. Unfortunately it seems that the Raycast isn’t hitting the player, who has a Character Controller attached.
I’ve googled, but none of the other answers seem to answer this. Any idea why Raycast wouldn’t hit the player?
[the yellow line is the Line drawn before the raycast, the cyan line is the line drawn between the enemy and the “hit” object. If the palyer was being hit, you’d see it in the consol and a lot of cyan lines, equal to the yellow lines.]
function OnTriggerStay (other : Collider)
{
if (other.gameObject.tag == "Player")
{
var rayDirection = other.gameObject.transform.position - transform.position;
rayDirection.y += 1;
var currentPosition = transform.position;
currentPosition.y += 1;
var hit : RaycastHit;
Debug.DrawLine (currentPosition, other.gameObject.transform.position, Color.yellow, 3);
if(Physics.Raycast(currentPosition, rayDirection, hit, viewDistance))
{
Debug.DrawLine (currentPosition, hit.collider.gameObject.transform.position, Color.cyan, 3);
print ("Hit: " + hit.collider.gameObject.transform.name);
if(hit.collider.gameObject.tag == "Player")
{
print("Player found");
}
}
}
}