Create an object with the LowLevelPhysics2D API
Create a 2D physics body, then attach shapes to the body.
Read time 3 minutesLast updated 12 days ago
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
- Segment, 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:
-
Create objects that hold the properties of the body and shapes. Make themfields so they display their properties in the
publicwindow. For example:Inspector?
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 useto get a definition object with the default values.PhysicsBody.defaultDefinition -
Use themethod of the world object to add a body to the world, and pass in the body definition.
CreateBodyPhysicsBody myObject = world.CreateBody(bodyDefinition); -
Create a shape using a geometry object, for example aobject. For example, the following creates a circle with a radius of 2 meters.
CircleGeometryCircleGeometry circleShape = new CircleGeometry { radius = 2f }; -
Attach the shape to the body using themethod of the body, and pass in the shape definition.
CreateShapemyObject.CreateShape(circleShape, shapeDefinition); -
If your script is in aclass attached to a GameObject, adjust the properties of the body, the geometry, and the shape in the Inspector window.
MonoBehaviourTo 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. -
Enter Play mode to run the script and create the objects. Unity displays the shape in the Scene view and Game view as a debug visualization. The line on the shape points towards the rotation direction of the shape.
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); }}