I have an enemy that follows a player if the player is withing a cone of vision. I need a way to stop the enemy from chasing the player if the player is behind something. I am trying to make a ray or spherecast that will go between player and enemy when the player is in the view of the enemy and if the ray or spherecast hits an object before hitting the player then the enemy should not know that the player is there. Here is a chink of the code that I am talking about.
if((Vector3.Angle(targetDirection, front) <= (fieldOfView / 2.0)) && ((pToE).magnitude <= appHorizon ))
{
//Debug.Log("Saw The Play Theif");
if(!seePlayer)
{
seePlayer = true;
}
if((!sawPlayer) && (seePlayer))
{
sawPlayer = true;
}
//Casts a elongated sphere with origin radius direction distance layermask[NOT USED OPTIONAL] [pToE is player to enemy (vector3)]
if(Physics.SphereCast(transform.position, transform.localScale.y, pToE, thingSeen, Vector3.Distance(playerPos, transform.position)))
{
if(thingSeen.collider.gameObject.tag == player.tag)
{
Debug.Log("found player");
}
else
{
Debug.Log(thingSeen.collider.gameObject.tag);
}
}
For some reason the player.tag is only shown for a few frames and then it goes away even if the player is still in the view of the enemy. In other words the “Found player” message only appears briefly and then goes away when the player is in the field of view. The Enemy does not see walls because the Debug.Log(thingSeen.collider.gameObject.tag); returns “untagged” message and never “cube” which is what every object other then player is named.