4

I have a little problem with BoxCollider2D. When I do this:

bool someBool = someBounds.Contains(somePoint);

I get expected result when the body has no rotation like on the picture 1. (red area is where somePoint makes someBool true) And when the body is rotated, somePoint makes someBool true everywhere inside the red area(picture 2). I don't want this kind of behaviour. I want point to return true inside the green area(picture 3).

Image

user3071284
  • 6,955
  • 6
  • 43
  • 57
Andy671
  • 75
  • 8
  • Is there a way to rotate the red box with the green? – AustinWBryan Feb 12 '16 at 11:06
  • the red box doesn't exist, it is just to show area where bounds contain somePoint. – Andy671 Feb 12 '16 at 11:14
  • The Unity BocXollider2D has a transform property. So u need to apply the same transformation to the collider as to the object. This is what I would try first. Not 100% sure. – Yosh Synergi Feb 12 '16 at 11:33
  • How are you rotating this Body? You need to make sure that you are rotating the BoxCollider2D too, and not expanding it. – Ricardo Reiter Feb 12 '16 at 12:24
  • The `BoxCollider2D` component belongs to the same `GameObject` of that body that you are rotating? – Ricardo Reiter Feb 12 '16 at 12:25
  • http://forum.unity3d.com/threads/rotating-sprite-does-not-rotate-collider.212327/ . Basically, rotate over 'z' only as well as do not flip over the body (as it's 2D). You can also try to set rigidbody to 'fixed'. If it doesn't help, use poly collider instead of 2D collider, but as the bug related to this issue dates back to 2006, hopefully it was not re-introduced so you won't need this workaround. –  Feb 12 '16 at 13:11
  • It is possible than the object image are a square? In the left up corner of your Scene View select Shaded Wireframe. – Ernesto Alfonso Feb 12 '16 at 13:30

1 Answers1

2

To accomplish your goal, use BoxCollider2D.OverlapPoint like this:

bool overlaps = myBoxCollider2D.OverlapPoint (somePoint);

The bounds of a BoxCollider2D assume that the object is an AABB - "axis-aligned bounding box", meaning it is not rotated and is aligned like in picture 1. So, really your pictures 1 and 2 prove this.

Source: If you go to the BoxCollider2D documentation, click on Bounds, then click on the Bounds class, you will see the details of what the bounds refer to.

user3071284
  • 6,955
  • 6
  • 43
  • 57