My monster kills the player if it comes too close. But it kills you through walls...

(Maybe my English isn´t perfect)

Hi, in my horror game, the monster kills the player if it comes too close. (If the player is in its trigger).

The problem is, it would kill me through walls.

Is it possible to detect if a mesh collider is between the monster and the player?

Thanks in advance :slight_smile:

Try using a raycast to confirm line of sight.

You may need multiple raycasts.

why don’t you make a ray from player to the monster

if( Physics.Raycast( ray, out hit ) )
{
     
    if( hit.collider.tag == "player" )
        //kill
}

slower but more accurate you can set number of rays you want

 Quaternion step = Quaternion.AngleAxis(2, Vector3.up);
 angle = transform.rotation * Quaternion.AngleAxis(-60, Vector3.up);
 direction=angle * Vector3.forward; 
    for(var i = 0; i < numberOfRays; i++)
         {
             if(Physics.Raycast(transform.position, direction, out hit, 2000))
             {
                 
                 if(hit.collider.tag=="player")
                 {
                      //kill
                 }
             }
             direction = step * direction;
         }
    }

the best way is to make rays from monsters eye to players body parts transform

 foreach(ray in rays)
      if( Physics.Raycast( ray, out hit ) )
        {
             
            if( hit.collider.tag == "player" )
                //kill
        }