- Home /
Physics2D.OverlapAreaAll/NoAlloc fails detecting object in puzzling manner
I have an issue with a 2D collision that fails to detect an object.
I am working on a 2D platformer, with sprite objects using box colliders. The main character has several scripts that controls his behavior; one for maintaining positions for collision purposes as Vector2Ds (like bottom, center, and right) and one for the main movement and collision detection. In order to detect an object, in this case a ladder, I use OverlapAreaNoAlloc with the characters position, detecting objects on a custom layer (512). The same issue is present with OverlapAreaAll. The ladders all have non-trigger box colliders.
However, in the test room I have made, the player fails to detect ladders with an x-coordinate between ~4.1 and ~8.2. Ladders before this and after that position works. The object itself does not matter - different objects behave identically, and moving one in and out of this zone, even at runtime, changes its behavior.
What is most puzzling is that I have isolated the problem to the origin of the float coordinates used. If I use the coordinates obtained through a reference to the other script, it fails, while if I base the coordinates on the transform.position, it finds the ladder. The only line of code (as well as a debug log) I change is the one below:
(fails)
int count = Physics2D.OverlapAreaNonAlloc(new Vector2(cols.VBack3.x, cols.VCenter.y), new Vector2(cols.VFront3.x, cols.VUp.y), ladders, ladderLayer);
Debug.Log("checking cords: (" + cols.VBack3.x + "," + cols.VCenter.y + "), (" + cols.VFront3.x + "," + cols.VUp.y + "), " + count);
(succeeds)
int count = Physics2D.OverlapAreaNonAlloc(new Vector2(transform.position.x - cols.Dist_Hor + 0.03f, transform.position.y), new Vector2(transform.position.x + cols.Dist_Hor - 0.03f, transform.position.y + cols.Dist_Vert), ladders, ladderLayer);
Debug.Log("checking cords: (" + (transform.position.x - cols.Dist_Hor + 0.03f) + "," + (transform.position.y + 0.13f) + "), (" + (transform.position.x + cols.Dist_Hor - 0.03f) + "," + (transform.position.y + cols.Dist_Vert + 0.13f) + "), " + count);
When positioning the character at roughly the same position (roughly 1 pixel difference), the above two debug logs prints out
checking cords: (7.523224,0.99), (7.573224,1.1), 0
checking cords: (7.531714,0.99), (7.581715,1.1), 1
with the objects to be found having collision at: (7.41,0.72), (7.57,1,68)
In other words, Physics2D.OverlapAreaAll and Physics2D.OverlapAreaNoAlloc magically knows when a number is from another script and stubbernly refuses to work within a particular area of about 400 pixels. For no reason. With a 100% fail rate. I am currently using Unity 4.5.1f3 (free version). Have I missed something completely here or is this a bug in Unity itself?
I'm looking forward to see if someone can clarify this issue.
Answer by Mikker · Aug 19, 2014 at 09:16 PM
Found the problem. Apparently Physics2D.OverlapArea doesn't work when the area is too small. Making the area larger fixes the problem.
An area of 0.05x0.11 is apparently too small for OverlapArea. This seems like a pretty severe flaw in Unity.
I'm having the same issue and noticing the same behavioral outcomes with size. I just switched from 4.3 pro to 4.5 free and started experiencing this problem. Very frustrating.
Your answer
Follow this Question
Related Questions
Colliders 2d apparently touch each other even if they should not. 2 Answers
Having problems with 2d collision triggers (javascript) 1 Answer
Colliders in a wall jut out 0 Answers
BoxCast, OverlapArea, and Raycasting; ground detection questions 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers