Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c# - How do I get all overlapped Collider2D on any layer in LayerMask array

I need to choose all elements in the array

public LayerMask[] ground;

I have a problem "Wrong number of indices inside []; expected 1", with:

a = Physics2D.OverlapCircle(bottom.position, radius, ground[0, ground.Length]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the logical OR | operator to combine the masks then do one OverlapCircle with that combined mask, and then you can call the override of OverlapCircle that will give you multiple results. You'll have to create a ContactFilter2D to do so:

// Initialize the results list once and keep it around to avoid unnecessary 
// garbage collection
List<Collider2D> overlapResults;

void Awake()
{
    overlapResults = new List<Collider2D>(); 
}

// ... in the method in question...

int allMask = 0; 
foreach (LayerMask lm in ground) 
{ 
    allMask = allMask | lm.value; 
} 
ContactFilter2D filter = new ContactFilter2D();
filter.SetLayerMask(allMask);

Physics2D.OverlapCircle(bottom.position, radius, filter, overlapResults);
for (Collider2D overlapResult in overlapResults) 
{
    // do stuff with overlapResult
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...