How to detect enemy in effective way?

Hello, developers.

Now, I have trouble with detecting enemy.
Most of coders use Raycast when they need something to detect specific thing.

But raycasting is not quite good, I think.

For example,

void ExampleCode()
{
...


RaycastHit hit;

if(Physics.Raycast(player.position, player.forward, out hit, 15))
 if(hit.transform.tag == "Enemy")
  Debug.Log("It's Enemy!");

...
}

21889-1.png

In this case, it is perfect what I wanted.

But, what if in this case?

21890-2.png

Then we should plus the code like player.back
But what if enemy is coming from 7’o clock?

So, ultimately, I want to make player detect enemy in specific area like this.

alt text

Is it possible with Raycast? or other ways? Thanks for reading.

You will need to use

Collider[] colliders = Physics.OverlapSphere(transform.position, radius);

Then just iterate through the colliders.

Two other options: use OverlapSphere and sort through returned colliders, or put a big sphere collider around the player (as trigger) and detect that way.