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:
int count =
Debug.Log(“checking cords: (” +
cols.VBack3.x + “,” + cols.VCenter.y +
“), (” + cols.VFront3.x + “,” +
cols.VUp.y + "), " + count);
int count =
cols.Dist_Hor + 0.03f,
cols.Dist_Hor - 0.03f,
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.