For what it's worth I had a small hiccup and wanted to share my solution with anyone that could use it. Previously, with 3D colliders and objects, we had a lovely Bounds object that contains the Extents of our object in x, y, and z. Great for plenty of things. Custom collisions, controllers, raycast origins, special effects, etc. It was as easy as: Code (csharp): Vector3 playerExtents = collider.bounds.extents; Unfortunately, 2D colliders (and renderers) don't seem to have a nice Bounds object to work with. We can, however, reference a BoxCollider2D, or any other 2D collider for that matter. After referencing this collider, we can just get the info we want from that. For example: Code (csharp): Vector3 extents = GetComponent<BoxCollider2D>().size * 0.5f; Which can be used to get player extent offsets like so: Code (csharp): Vector3 PlayerBottomLeft = new Vector3(-extents.x, -extents.y, 0f); Vector3 PlayerBottomRight = new Vector3(extents.x, -extents.y, 0f); And used like so: Code (csharp): float rayDist = 2.0f; // Distance to cast rays // Raycast down from bottom left Vector3 rayOrigin = transform.position + PlayerBottomLeft; Vector3 rayDirection = Vector3.down; RaycastHit2D hitBottomLeft = Physics2D.Raycast(rayOrigin, rayDirection, rayDist, groundMask); // groundMask is a simple LayerMask Debug.DrawRay(rayOrigin, rayDirection * rayDist * hitBottomLeft.fraction, Color.yellow); Hopefully this will help someone out! Also, please let me know if I've missed something.
Unfortunately, retrieving the collider bounds for 2D didn't make it into the 4.3 release however it has already been added and will be available in a future release. Hope this helps.
Any word on this? Will it be a major release or can we expect it to appear any time soon? Having to use transform to check for collisions is kinda sucky.
It's not scheduled for a major release so you won't have to wait for that. All improvements are going into releases after 4.3 though.
It will be in the 4.5 release as will a majority of the changes that have been spoken about in the forums.
I was trying to figure out where the feet and top of my sprites are so I can have them bop each other Mario style and could not figure out how to get an accurate collision height with 2d. Thanks!
Wait, PolygonCollider2D does not have a size variable. I guess I will just have to loop through PolygonCollider2D.points and find the highest one?
I add this code to compute object bounds if someone come here searching a solution. Code (csharp): // compute bounds in local space public static Bounds BoundsOf(Collider2D collider) { var bounds = new Bounds(); var bc = collider as BoxCollider2D; if (bc) { var ext = bc.size * 0.5f; bounds.Encapsulate(new Vector3(-ext.x, -ext.y, 0f)); bounds.Encapsulate(new Vector3(ext.x, ext.y, 0f)); return bounds; } var cc = collider as CircleCollider2D; if (cc) { var r = cc.radius; bounds.Encapsulate(new Vector3(-r, -r, 0f)); bounds.Encapsulate(new Vector3(r, r, 0f)); return bounds; } // others :P Debug.LogWarning("Unknown type "+bounds); return bounds; } // return bounds in world space public static Bounds BoundsColliders(GameObject obj) { var bounds = new Bounds(obj.transform.position, Vector3.zero); var colliders = obj.GetComponentsInChildren<Collider2D>(); foreach(var c in colliders) { var blocal = Utils.BoundsOf(c); var t = c.transform; var max = t.TransformPoint(blocal.max); bounds.Encapsulate(max); var min = t.TransformPoint(blocal.min); bounds.Encapsulate(min); } return bounds; } This will help to test Code (csharp): void OnDrawGizmos() { if (enabled) { Gizmos.color = color; var bounds = Utils.BoundsColliders(gameObject); Gizmos.DrawWireCube(bounds.center, bounds.size); } }