Version: Unity 6.3 LTS (6000.3)
Language : English
Create a world with the LowLevelPhysics2D API
Add a sprite to a LowLevelPhysics2D API object

Create an object with the LowLevelPhysics2D API

Note: This documentation is about writing C# scripts using the LowLevelPhysics2D API. To use 2D physics in the Unity Editor using components like the Rigidbody 2D component, refer to 2D physics instead.

After you create a physics world, you can add physics objects to the world.

An object is usually made up of two parts:

  • A physics body, which defines the position, rotation, and velocity of the object. It doesn’t define an area.
  • One or more physics shapes attached to the body, which define the area that interacts with other shapes. Unity also automatically draws the shapes as a debug visualization.

You can add any number of shapes to a physics body. You can create shapes with the following geometries:

  • Circle
  • Capsule
  • Polygon
  • SegmentSegments are subsets of your player base, split apart by key differentiators. Viewing metrics and events by segment can reveal differences in-game behavior between different groups. More info
    See in Glossary
    , which creates a line
  • Chain segment, which you use to create a series of connected line segments

Create a physics body and physics shape

Follow these steps:

  1. Create objects that hold the properties of the body and shapes. Make them public fields so they display their properties 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. For example:

    public PhysicsBodyDefinition bodyDefinition = new PhysicsBodyDefinition();
    public PhysicsShapeDefinition shapeDefinition = new PhysicsShapeDefinition();
    

    A new definition has a set of default values. For more information about definitions and changing the default values, refer to Configure 2D physics properties using a definition.

    You can also use PhysicsBody.defaultDefinition to get a definition object with the default values.

  2. Use the CreateBody method of the world object to add a body to the world, and pass in the body definition.

    PhysicsBody myObject = world.CreateBody(bodyDefinition);
    
  3. Create a shape using a geometry object, for example a CircleGeometry object. For example, the following creates a circle with a radius of 2 meters.

    CircleGeometry circleShape = new CircleGeometry { radius = 2f };
    
  4. Attach the shape to the body using the CreateShape method of the body, and pass in the shape definition.

    myObject.CreateShape(circleShape, shapeDefinition);
    
  5. If your script is in a MonoBehaviour class attached to a 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
    , adjust the properties of the body, the geometry, and the shape in the Inspector window.

    To configure the objects in your script instead, set the properties of the definition object before you create the objects. For more information, refer to Configure 2D physics properties using a definition.

  6. Enter Play mode to run the script and create the objects. Unity displays the shape 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 and Game view as a debug visualization. The line on the shape points towards the rotation direction of the shape.

Note: By default, a physics body is static and isn’t affected by physics. To make it dynamic, in the Inspector window of the GameObject, set Body TypeDefines a fixed behavior for a 2D Rigidbody. Can be Dynamic (the body moves under simulation and is affected by forces like gravity), Kinematic (the body moves under simulation, but isn’t affected by forces like gravity) or Static (the body doesn’t move under simulation). More info
See in Glossary
to Dynamic. For more information, refer to Configure objects with definitions.

After you create a shape from geometry, the shape doesn’t change if you change the geometry object.

Example

The following example creates a circle. Attach the script to a GameObject in your scene then enter Play mode to check the shape.

using UnityEngine;
using UnityEngine.LowLevelPhysics2D;

public class CreateWorldAndObjects : MonoBehaviour
{
    // Declare definitions that contain default properties for the body and shape
    public PhysicsBodyDefinition bodyDefinition = PhysicsBodyDefinition.defaultDefinition;
    public PhysicsShapeDefinition shapeDefinition = PhysicsShapeDefinition.defaultDefinition;
 
    void Start()
    {
        // Get the default world
        PhysicsWorld world = PhysicsWorld.defaultWorld;

        // Create the physics body with the body definition
        PhysicsBody myObject = world.CreateBody(bodyDefinition);

        // Create the circle geometry
        CircleGeometry circleGeometry = new CircleGeometry { radius = 1.5f };

        // Create the shape with both the geometry and the shape definition
        myObject.CreateShape(circleGeometry, shapeDefinition);
    }
}

Additional resources

Create a world with the LowLevelPhysics2D API
Add a sprite to a LowLevelPhysics2D API object