- Home /
How to fix overlapCircle and LayerMask problem?
Still a noob
My game is a 2d top down, and there are multiple plane flying randomly, I want them to detect nearby plane, to trigger a warning. You see, I'm trying to use the overlapCircle to detect nearby obj, simple right? But to set the LayerMask, all of my plane will be in the same layer "Plane", so when I use overlapCircle, it detect itself? Is there a way to fix this problem?
Answer by TheKnightsofUnity · Nov 23, 2018 at 02:59 PM
Hello there, @Rising_Dawn5121995!
To detect other planes in the range, we suggest to use Physics2D.OverlapCircleAll instead. This returns an array of found Colliders, which you can use to check if there are any other planes in the trigger range.
private void CheckCollision(Vector2 position, float radius)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(position, radius);
foreach (Collider2D collider in colliders)
{
if (collider.gameObject == this.gameObject)
{
continue;
}
else
{
// Trigger a warning
break; // Remove this if you want the warning to trigger for each plane in range
}
}
}
All the best,
The Kinghts of Unity
Your answer
Follow this Question
Related Questions
LayerMask help 2 Answers
How to use Physics2D.OverlapArea 2 Answers
Raycast should ignore everything but player, yet still gets interrupted 1 Answer
Physics.OverlapSphere 2 Answers
How to not instantiate an object if there is another object there? 1 Answer