Bounds.Contains

Hi,

I am trying to use Bounds.Contains to find out if a point is within a BoxCollider2D. But it doesn’t appear to be working.

                Vector3 newPosition = transform.TransformPoint(new Vector3(transform.position.x, transform.position.y, transform.position.z));
                float x = newPosition.x;
                float y = newPosition.y;

                if (area.bounds.Contains(new Vector3(x, y, 0)))
                {
                     hasTarget = true;
                 }

Like I can see from the editor that the point I am looking for (transform) is contained within the boxcollider2d, but the script is telling me its not

EDIT:

Just thought I would upload what I am working with

I am trying to check if the rat is in the BoxCollider2D

BoxCollider2D details:

107404-details.png

You have two main problems which we can only deduce by the bits of extra information that you’ve given throughout your comments here.

First of all your calculation of your “newPosition” makes no sense. transform.position is already the position of the object in world space. TransformPoint transforms a local space position into worldspace. Since you pass in a worldspace position the outcoming position is nonsense. So like @JVLVince said in the comment below his answer you just use the object’s position.

Next thing is the Bounds struct is a 3d construct. So Contains always works with 3d coordinates. As we can see in your screen shot in one of your comments your Bounds center is at (8.0, -21.2, -0.7). Since the extents is (17.2, 6.3, 0) it will represent a “thin” box with 0 thinkness which is located at z “-0.7”. Of course when you pass in a position (x, y, 0) it will never be inside the bounds.

You could tranform the 3d bounds into a “2d bounds” which simply ignores the z value.

public static class Bounds2DExt
{
    public static Bounds Get2DBounds(this Bounds aBounds)
    {
        var ext = aBounds.extents;
        ext.z = float.PositiveInfinity;
        aBounds.extents = ext;
        return aBounds;
    }
}

This will simply set the z value of the extents to infinity so the 3d box goes from negative infinity up to positive infinity so the z value doesn’t matter. With this extension class somewhere in your project you can simply do:

if (area.bounds.Get2DBounds().Contains(transform.position))

This should work with any 2d value regardless of what z value it has.

Here is a line of code from a game I am working on, that works just as expected…

if(DPadLeft.bounds.Contains(touch.position))

DPadLeft is a Collider2D and touch.position is a Vector2

So, in your code, I assume area is the BoxCollider2D? try using a Vector2 instead of 3 in your contains, and see if that works.

Hope this helps,
-Larry

If every things inside scene is just fine and you got the wrong result, I think this must be the cause. You should take a look at bounds document.

Note: If Bounds.extents contains a negative value in any coordinate then Bounds.Contains will always return False.

@caulfield85

Try this:

Vector2 playerPosition = transform.position;

if (Collider2D.bounds.Contains (playerPosition)) {
    hasTarget = true;
}