https://docs.unity3d.com/560/Documentation/ScriptReference/ContactFilter2D.html I don't understand this at all. I'm trying to use the getcontacts/overlapbox or the overlapcollider and I have no clue what they want me to provide. Let's say I have 3 overlapping boxes, how do I use any of the above to get a result with all the overlapping boxes? If anyone has a code example it would be most appreciated. Thx
It's basically a way to avoid using OnEnter or OnStay calls because you can gather that data yourself instead. For example I use it to create my own physics based character controller. Is there a particular reason you need to filter and deal with lower level collision data? What is your actual issue, seeing as you don't seem to know why you are using this feature. The documentation explains how it can be used with everything. For example you want to gather some contacts from a rigidbody with collider potentially touching something? Use int count = rb.GetContacts(contactFilter, contacts); And then loop through the results. But really, explain your actual problem, there's probably much better ways.
I just need the best way to see which other 2d boxes are overlapping with a given box at any point in time. I was using triggerenter to add the detected object to a list, but then I saw those functions. It seemed like the better way to go about it, but I can't get it working to test if it's actually more useful or not.
https://docs.unity3d.com/ScriptReference/Physics2D.BoxCastNonAlloc.html Loop through results and reject anything that does not interest you. More: https://docs.unity3d.com/ScriptReference/Physics2D.html - there's a heck of a lot of ways to skin this.
K, I think I figured it out mate. I just didn't know what I needed to provide for (contactFilter, contacts). When I made an empty array for contacts it just didn't work or returned 0. But after I did contactFilter.nofilter() it started debugging the amount of boxes that were overlapping. Thanks for your help anyway, much appreciated.
It clearly says in the documentation that you need to provide an array that has the capacity to return results; arrays cannot be resized. If you pass an empty array you'll get zero results. Try giving the docs a read. For GetContacts to work, there has to be contacts available i.e. the colliders in question are to set contact each other. If you look at the docs you'll see you do not even have to pass the ContactFilter2D. There's also a GetContacts that only returns the Collider2D rather than all the ContactPoint2D which is wasted time if you don't need them. You can also check to see if a specific collider is overlapping (ignoring any current contacts) by using Collider2D.OverlapCollider.
I didn't say I didn't give it a size. The solution was I had to say nofilter() cause just declaring an empty contactfilter caused it to still filter the results. And I don't have to give a contactfilter for getcontacts(), but I do need to give one for OverlapCollider(), even if I don't want anything filtered. Obviously I don't need these functions if I don't want to filter anything, but I was just trying to make it log the correct results.
But you did which is why I pointed you to the docs when you said: An empty array is zero size so no results can be returned. Yes, a default C# struct sets fields to their defaults which isn't a problem apart from the property "useTriggers" which defaults to false therefore by default, a ContactFilter2D will filter out triggers. It won't perform any other type of filtering however.
I guess I worded it badly, I just meant declare an empty array like Code (CSharp): Collider2D[] collidersArray = new Collider2D[5]; It returned 0 because of the default ContactFilter2D. Anyway I understand how it works now, thanks for all the help.
Wow...you wake up on the wrong side of the bed or are you always this passive aggressive with your end-users?
Because I used the world clearly? Or because you read it in an aggressive voice perhaps? Also, is this your only input to this thread?
Hi Le Hi Lejean, I had some issue with the wording as well (whats with declaring it an INT to begin with???) its a bit of a brute force approach but I declared the "contactFilter" as a public variable then you can mess around with the settings in the unity editor, this is an example how I've used it in my code (work in progress) targetChecker.OverlapCollider(targetFilterLeft, targetsInRangeLeft); for (int i = 0; i < targetsInRangeLeft.Length; i++) { if (targetsInRangeLeft != null) { Debug.Log("Left Position= " + targetsInRangeLeft.transform.position + " Left Target= " + targetsInRangeLeft.name); System.Array.Clear(targetsInRangeLeft, i, targetsInRangeLeft.Length); } } As you can see in the picture above I've got ready access to the various settings, these restrict what gets added to the array in the first place, i assume the boxes have a collider2D attached to them. @MelvMay the bold font in the first sentence made it seem passive aggressive, plus the fact that the manual is not massively informative as you seem to think, more like a size spec without the measurement type being stated(see tesco website)
Sorry, I'm confused; what has the manual got to do with this post? I was discussing a very specific API reference (https://docs.unity3d.com/ScriptReference/Collider2D.GetContacts.html) and not the manual. I do not understand your Tesco/size reference either, sorry. Kind of wondering why you felt the need to mention me in a post from June too. This is the content that is used for all API calls that accept an array to populate which I emphasized was clearly stated, not aggressive in any way although almost anything can be read that way if wanted. Text sucks sometimes.
Smart people don't have time for wasting time on things like passive/aggressive. Admittedly this can come across being cold or give regular people the wrong impression. Don't expect it by default on this forum to be the case Any further questioning of the developers wording will have the post removed and the reply banned, as it's clearly not in the interests of solving problems in the thread and off topic. If you have an actual problem with how someone uses the English language in text, use the PM system or the report system.
Not questioning, also wrote my post in a hurry this morning, sorry Was just saying I also read it as passive aggressive and MelvMay seemed unaware as to why anyone would see it as such, glad its not love Unity and have had great interactions with the staff and enjoy the great materials you put out. I meant the API sorry Melv (if I can call you that? ) was just in a rush so didn't check it properly. It is useful for giving details of the item but as someone who isn't very clued up to exactly how Unity operates it isn't particularly useful for working out how to use it. Again not questioning just felt the need to clarify b1nary wasn't on his own reading the wrong intent of the emphasis