I have a a player and a guard with walls. I have the guard that has a collision circle in front of it. I want that when the player is seen, Debug.Log(“I see you”), but if the wall is before the player (so the player is behind a wall) I don’t it to say anything.
right now I have: (which doesnt work if my player is behind a wall)
void OnTriggerStay(Collider otherObject)
{
if (otherObject.tag == "Player")
{
Debug.Log("I see you");
}
}
Add in a quick linecast between the player and the guard-
if(Physics.Linecast(guard.position, player.position))
{
// Behind a wall, can't see you
}
Or, I guess, more specifically-
if(Physics.Linecast(transform.position, otherObject.transform.position))
{
// Can't see you, won't attack you
}
And then to check whether the guard is actually looking in your direction (probably do this first, before the linecast, because it's cheaper)
if(Vector3.Angle(guard.forward, player.position - guard.position) > 90)
{
// Player is out of view
}