# CreateMesh(bool, bool, bool)

> Creates a planar Mesh that is identical to the area defined by the Collider2D geometry.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.0/script-reference/unityengine.md)
* **Assembly:** UnityEngine.Physics2DModule

```csharp
public Mesh CreateMesh(bool useBodyPosition, bool useBodyRotation, bool useDelaunay = true)
```

### Parameters

**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Should the mesh be transformed by the position of the attached [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md)?**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Should the mesh be transformed by the rotation of the attached [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md)?**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): When true, Delaunay triangulation is used to generate the mesh. This can reduce the number of vertices created in the Collider mesh and reduce the number of small triangle fans produced, both of which can improve overall mesh size and performance.

### Returns

| Type                                                        | Description                                                                                                                                                                                                   |
| ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) | The planar [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) created that matches the collider geometry or NULL if no [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) could be created. |

### Remarks

In simple terms, this method will create a [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) that exactly represents the area defined by the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md). The created [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) can then be used for any purpose including but not limited to navigation meshes for 2D navigation. The [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) does not own the created [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) and you are responsible for its lifetime therefore not deleting the [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) will result in a memory leak. The [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) is not altered in any way during this call. The [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) must have valid shapes present for a [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) to be created otherwise NULL will be returned indicating that no [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) was created.

All [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) geometry lives in the space of the [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) it is attached to. You can select whether the [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) vertices are transformed by the [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) position and rotation using useBodyPosition and useBodyRotation respectively. If the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) is not attached to a [Rigidbody2D](/engine/6000.0/script-reference/unityengine/rigidbody2d.md) then the geometry is permanently in world-space and both useBodyPosition and useBodyRotation should always be set to false.

What follows is more detail on how the [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) shapes are considered when creating a [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md). Whilst there are many types of [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md), they only produce collision geometry which comprise of the following primitive shape types: Circle, Capsule, Polygon and Edge. These primitive types are split into two groups known as closed convex primitives and open primitives. Primitives of type Circle, Capsule and Polygon are closed convex primitives which define a closed convex area bounded by the primitive. The Edge primitive is an open type that defines geometry with no internal area.

A [CircleCollider2D](/engine/6000.0/script-reference/unityengine/circlecollider2d.md) will produce a single circle primitive. A [CapsuleCollider2D](/engine/6000.0/script-reference/unityengine/capsulecollider2d.md) will produce a single capsule primitive. A [BoxCollider2D](/engine/6000.0/script-reference/unityengine/boxcollider2d.md) will produce a single polygon primitive. A [PolygonCollider2D](/engine/6000.0/script-reference/unityengine/polygoncollider2d.md) can produce multiple polygon primitives to convert a potentially concave area into multiple convex polygons. An [EdgeCollider2D](/engine/6000.0/script-reference/unityengine/edgecollider2d.md) can produce multiple edge primitives. A [TilemapCollider2D](/engine/6000.0/script-reference/unityengine/tilemaps/tilemapcollider2d.md) can produce multiple polygon primitives (per-tile). A [CompositeCollider2D](/engine/6000.0/script-reference/unityengine/compositecollider2d.md) can produce either multiple polygon primitives (in Polygon mode) or produce multiple edge primitives (in Outline mode).

When creating a [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) to represent [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) geometry, all closed convex primitives (circle, capsule and polygon) produce the respective filled area defined by those primitives. In the case where there are multiple polygons, the total area of all the polygons is created. Note that when [BoxCollider2D.edgeRadius](/engine/6000.0/script-reference/unityengine/boxcollider2d/edgeradius.md) that is greater than zero is used (to produce radial edges), the [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) is created to also represent that geometry.

When creating a [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) to represent [Collider2D](/engine/6000.0/script-reference/unityengine/collider2d.md) geometry, all open primitives (edges) have special handling. When the start vertex of the first edge is coincident with the end vertex of the last edge (to define a pseudo-closed area) then a mesh is created to represent that closed area even though 2D physics doesn't itself treat it as such. If the vertex are not coincident then a [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) will only be created if [EdgeCollider2D.edgeRadius](/engine/6000.0/script-reference/unityengine/edgecollider2d/edgeradius.md) is greater than zero as this produces edges with an area.

An [EdgeCollider2D](/engine/6000.0/script-reference/unityengine/edgecollider2d.md) when used with coincident start/end vertex will create a [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) that is a convex hull of all edge vertices. Any concave vertex (producing a concave configuration) will be considered as being on the convex hull resulting in a convex planar [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md). If a convex [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) is required then multiple polygon primitives should be used i.e. a [PolygonCollider2D](/engine/6000.0/script-reference/unityengine/polygoncollider2d.md) or a [CompositeCollider2D](/engine/6000.0/script-reference/unityengine/compositecollider2d.md) in polygon mode. A [CompositeCollider2D](/engine/6000.0/script-reference/unityengine/compositecollider2d.md) in outline mode produces edges with coincident start/end vertex so will always create a closed shape [Mesh](/engine/6000.0/script-reference/unityengine/mesh.md) which also supports [CompositeCollider2D.edgeRadius](/engine/6000.0/script-reference/unityengine/compositecollider2d/edgeradius.md).

Additional Resources: [Collider2D.GetShapeHash()](/engine/6000.0/script-reference/unityengine/collider2d/getshapehash.md).
