Configure LowLevelPhysics2D API objects with definitions
Create a definition object to configure properties such as gravity, friction, position, and shape.
Read time 4 minutesLast updated 13 days ago
To configure the properties of a physics world and its objects, such as gravity, friction, position, and shape, use definitions. Definitions are objects that store physics properties and values. You create a definition, then pass it into the world, body, or shape you create.
Definitions optimize physics performance by avoiding you setting properties after you create a world, body, or shape, which can make the CPU do extra work. Definitions also allow you to pass around and reuse sets of values.
For the full list of definition objects, refer to the API documentation.
LowLevelPhysics2DCreate and use a definition
Follow these steps:
-
Create a public definition object in your script, for example aobject to configure a
PhysicsBodyDefinition.PhysicsBody// Create a new body definition with default properties and valuespublic PhysicsBodyDefinition bodyDefinition = new PhysicsBodyDefinition();A new definition has a set of default values. To change the default values you receive, refer to the Change the default definition values section. -
Pass in the definition when you create the physics object. For example:PhysicsBody myObject = world.CreateBody(bodyDefinition);
If your script is in a class attached to a GameObject, Unity displays the properties of the definition in the window so you can configure them. For the full list of default properties, refer to Definitions reference for the LowLevelPhysics2D API.
MonoBehaviourInspector
?
If you change the definition after you create an object with it, the changes don't affect the existing object.
Set a definition property in a script
To configure the definition in your script, set the properties of the definition before you create the object with it.
For the full list of properties, refer to the API documentation.
LowLevelPhysics2DFor example:
public PhysicsBodyDefinition bodyDefinition = new PhysicsBodyDefinition{ // Set the linear velocity linearVelocity = Vector2.right * 4f, // Set the gravity scale gravityScale = 0f};// Create the bodyPhysicsBody myObject = world.CreateBody(bodyDefinition);
Create multiple objects with the same definition
To create multiple physics bodies or physics shapes with the same configuration, call the method of your world object. Pass in the definition and the number of objects you want to create. For example:
CreateBodyBatch// Create 500 bodies using the single definitionNativeArray<PhysicsBody> fiveHundredBodies = world.CreateBodyBatch(bodyDefinition, 500);
Change the default definitions
To change the default values you receive when you use or to create a definition object, follow these steps:
new.defaultDefinition- Create a Physics Low Level Settings 2D asset. For more information, refer to Configure global 2D physics settings.
- Select the asset in the Project window.
- In the Inspector window, configure the properties in the Default Definitions section.
For a full list of properties, refer to Physics Low Level Settings window reference.
Example
The following example uses a body definition to set the position of a physics body, then a shape definition to set the density of a shape.
using UnityEngine;using UnityEngine.LowLevelPhysics2D;public class CreateObjectsWithDefinitions : MonoBehaviour{ PhysicsWorld world; // Declare definitions with default properties for the body and shape public PhysicsBodyDefinition bodyDefinition = new PhysicsBodyDefinition(); public PhysicsShapeDefinition shapeDefinition = new PhysicsShapeDefinition(); void Start() { // Get the default world world = PhysicsWorld.defaultWorld; // Set the position of the body using the body definition bodyDefinition.position = new Vector2(0f, 5f); // Create the physics body with the body definition PhysicsBody myObject = world.CreateBody(bodyDefinition); // Set the density of the shape to 5 kg/m² shapeDefinition.density = 5f; // 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); }}