Version: Unity 6.3 LTS (6000.3)
Language : English
Crop a sprite
Sprites sorting order

Create collision shapes for a sprite

In a 2D project, Unity uses collisionA collision occurs when the physics engine detects that the colliders of two GameObjects make contact or overlap, when at least one has a Rigidbody component and is in motion. More info
See in Glossary
geometry to determine if a spriteA 2D graphic objects. If you are used to working in 3D, Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development. More info
See in Glossary
collides with other sprites. Collision geometry can be one shape, for example a circle around the sprite, or a set of multiple shapes.

You can do either of the following:

  • Generate collision geometry automatically.
  • Create default custom collision geometry for all instances of a sprite.

Generate collision geometry automatically

Follow these steps:

  1. From the Project window, drag the sprite asset () into the scene.
  2. In the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
    See in Glossary
    window of the sprite GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
    See in Glossary
    , select Add Component.
  3. Select Physics 2D, then a Collider 2D component.

If you select Polygon ColliderAn invisible shape that is used to handle physical collisions for an object. A collider doesn’t need to be exactly the same shape as the object’s mesh - a rough approximation is often more efficient and indistinguishable in gameplay. More info
See in Glossary
2D
, by default Unity tries to create collision geometry that encompasses the opaque parts of the sprite.

Unity displays the collision geometry as a green outline in the SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
view. To edit the collision geometry, refer to the Edit the collision geometry for one instance section.

Create default collision geometry for all instances of a sprite

To create custom collision geometry that Unity uses every time you create an instance of a sprite, use the Custom Physics Shape tab of the Sprite Editor window and a Polygon Collider 2D component.

Follow these steps:

  1. In the Hierarchy window, select the sprite GameObject.

  2. In the Inspector window, in the Sprite RendererA component that lets you display images as Sprites for use in both 2D and 3D scenes. More info
    See in Glossary
    component, select Open Sprite Editor.

  3. Select the Custom Physics Shape tab in the top-left dropdown. Unity displays the Outline Tools overlay.

  4. Select Generate to automatically generate an outline that follows the opaque parts of the sprite. Unity displays the outline in white. Each point is a vertex of a collision shape.

    You can also create one or more outlines manually by clicking the sprite then dragging a rectangle.

  5. Edit the geometry if you need to. For more information, refer to Custom Physics Shape tab reference for the Sprite Editor window.

    To change how closely the geometry follows the opaque parts of the sprite, adjust the Outline Detail and Alpha Tolerance properties, then regenerate the outline.

  6. To save the geometry, select Apply in the toolbarA row of buttons and basic controls at the top of the Unity Editor that allows you to interact with the Editor in various ways (e.g. scaling, translation). More info
    See in Glossary
    .

  7. Add the sprite to the scene.

  8. In the Inspector window of the sprite GameObject, select Add Component, then Physics 2D > Polygon Collider 2D.

Left: Automatically generated geometry with a low Outline Detail value. Right: Automatically generated geometry with a higher Outline Detail value.
Left: Automatically generated geometry with a low Outline Detail value. Right: Automatically generated geometry with a higher Outline Detail value.

Unity now uses the collision geometry for all new instances of the sprite if you add a Polygon Collider 2D component.

Note: Unity doesn’t automatically update existing GameObjects with the collision geometry. To update an existing Polygon Collider 2D component, right-click the title of the collider component in the Inspector window, then select Reset.

For more information, refer to Custom Physics Shape tab reference for the Sprite Editor window.

Edit the collision geometry for one instance of a sprite

To edit the auto-generated collision geometry of a single sprite instance, or override the collision geometry from the Custom Physics Shape tab, use the Edit Collider button.

Follow these steps:

  1. In the Hierarchy window, select the sprite GameObject.
  2. Select Edit Collider (). The Edit Collider button is also available in the Tools overlay of the Scene viewAn interactive view into the world you are creating. You use the Scene View to select and position scenery, characters, cameras, lights, and all other types of Game Object. More info
    See in Glossary
    .

To edit the geometry, do the following:

  • To move a point, select and drag it.
  • To add a point, click on an edge.
  • To remove a point, hold Ctrl (macOS: Cmd) while hovering over an edge, then select a red edge.
  • To exit the editing mode, select Edit Collider again.

Note: Editing the collision geometry of a single instance doesn’t change the collision geometry in the Custom Physics Shape tab of the Sprite Editor window.

Additional resources

Crop a sprite
Sprites sorting order