- Home /
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!");
...
}
In this case, it is perfect what I wanted.
But, what if in this case?
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.
Is it possible with Raycast? or other ways? Thanks for reading.
Answer by Mike-Geig · Feb 09, 2014 at 12:46 AM
You will need to use
Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
Then just iterate through the colliders.
Answer by getyour411 · Feb 09, 2014 at 12:46 AM
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.
I also thought about 2nd method, but if I use that, Player's hit range would be increasing so much. 1st method was the thing I never think. Thanks!
A big ol sphere collider as a trigger has nothing to do with "hit range" (you do with it whatever you want) and it's really essentially the same thing as OverlapSphere - but I'm glad you have a solution.
Will a trigger collider put strain on the Physics engine?