Create a 2D 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 that simulates physics using the LowLevelPhysics2D API.
Follow these steps:
In a C# script, add using UnityEngine.LowLevelPhysics2D; at the top to include the LowLevelPhysics2D library.
You can use the LowLevelPhysics2D API in any C# script, not only MonoBehaviour script files. But if you use a MonoBehaviour script file, you can configure physics 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 of 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, move GameObjects, and attach spritesA 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.
To add 2D physics objects, you first need to create a PhysicsWorld object to add them to. Do either of the following:
For more information, refer to Create a physics world.
To add physics objects to the world, do the following:
PhysicsBody object, which defines the position, rotation, and velocity of an object. It doesn’t define an area.PhysicsShape objects to the PhysicsBody, which define the area that interacts with other shapes. Unity also draws the shapes as a debug visualization.For more information, refer to Create a physics object.
To configure the properties of the world and its physics objects, create definition objects, for example PhysicsBodyDefinition. Definition objects contain properties like position and gravity that you can adjust in the Unity Editor, or pass into the object when you create it.
To configure the properties in the Inspector window, make the definition object a public field.
For more information, refer to Configure objects using definitions.
To start the simulation, you usually attach the script to a GameObject in your scene, then enter Play mode.
By default, physics objects don’t move a GameObject. To make a physics object update the Transform componentA Transform component determines the Position, Rotation, and Scale of each object in the scene. Every GameObject has a Transform. More info
See in Glossary of the GameObject, refer to Move a GameObject with the LowLevelPhysics2D API.
To add a sprite, refer to Add a sprite to an object.
The following example creates a small circle that falls under gravity onto a large circle. Attach the script to a GameObject in your scene, then enter Play mode.
using UnityEngine;
using UnityEngine.LowLevelPhysics2D;
public class Example2DPhysics : MonoBehaviour
{
void Start()
{
// Create the world
PhysicsWorld world = PhysicsWorld.defaultWorld;
// Create a body in the world, with a definition object that sets the position and enables physics
PhysicsBody object1 = world.CreateBody(new PhysicsBodyDefinition
{
position = new Vector2(0.5f, 8f),
type = PhysicsBody.BodyType.Dynamic
});
// Attach a circle shape to the first body
PhysicsShape object1shape = object1.CreateShape(new CircleGeometry());
// Create a second body in the world, with a definition object that sets the position and disables physics
PhysicsBody object2 = world.CreateBody(new PhysicsBodyDefinition
{
position = new Vector2(0f, 0f),
type = PhysicsBody.BodyType.Static
});
// Attach a circle shape to the second body
object2.CreateShape(new CircleGeometry {
radius = 3f,
});
}
}