Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
ClosephysicsShapeGroup | The PhysicsShapeGroup2D to store the retrieved PhysicsShape2D in. |
int Returns the number of PhysicsShape2D retrieved from the Collider2D.
Gets all the PhysicsShape2D used by the Collider2D.
All Collider2D represent a high-level definition of 2D geometry that is used to create efficient low-level primitive shapes used by the physics engine for collision detection. These low-level primitive shapes are what the 2D physics gizmos draw within the Unity Editor. This method provides access to a copy of these primitive shapes which can be useful in various use-cases such as debugging and gizmos or rendering Collider2D geometry at runtime.
Use this method to gain access to a copy of all the low-level primitive shapes that are produced by a Collider2D. If no such shapes exist on the Collider2D as indicated by Collider2D.shapeCount then the shape group will not be populated with shapes. If shapes do exist, the group is first emptied before being populated with shapes.
Each Collider2D produces either a single or multiple PhysicsShape2D of a single PhysicsShapeType2D as indicated below:
All Collider2D shape vertices are stored in the local space of the Rigidbody2D they are attached to i.e. their vertices are relative to the pose of the Rigidbody2D therefore when a Rigidbody2D translates or rotates, the vertices of attached Collider2D do not change. This is why changes to position or rotation should only ever happen via the Rigidbody2D as this is the most efficient method. When retrieving shapes, the shape vertices are in the local space of the Rigidbody2D. The current position and rotation of the Rigidbody2D is stored in PhysicsShapeGroup2D.localToWorldMatrix and can be used to transform the shape vertices to world space. If the Collider2D is not attached to any Rigidbody2D i.e. it is Static then the local space and world space are identical therefore the PhysicsShapeGroup2D.localToWorldMatrix is set to Matrix4x4.identity.
Note: It is recommened to reuse the PhysicsShapeGroup2D because then memory allocations only occur if the number of shapes or vertices retrieved are greater than the existing capacity of the PhysicsShapeGroup2D. Reusing this object therefore minimizes allocations to a bare minimum and eventually to none so no work is produced for the garbage collector.
Additional resources: Rigidbody2D.GetShapes.
physicsShapeGroup | The PhysicsShapeGroup2D to store the retrieved PhysicsShape2D in. |
shapeIndex | The index of the first shape to retrieve. This should be in the range of 0 to Collider2D.shapeCount-1. |
shapeCount | The number of shapes to retrieve. The shapeIndex + shapeCount must be less than or equal to Collider2D.shapeCount. |
int
Returns the number of PhysicsShape2D retrieved from the Collider2D. In all cases this should be the same number as shapeCount
.
Gets the specified range of the PhysicsShape2D used by the Collider2D.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.