Note: This documentation is about writing C# scripts using the Unity.U2D.Physics API. To use 2D physics in the Unity Editor using components like the Rigidbody 2D component, refer to 2D physics instead.
By default, when you create an object with the Physics Core 2D API, the object isn’t connected to GameObjects 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.
To move a GameObject, configure the physics object to 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.
Follow these steps:
To enable physics bodies updating Transform components globally, set the TransformWriteMode property of the world to Fast2D or Slow2D.
To enable the physics body updating Transform components, set its TransformWriteMode to Current.
To attach the Transform component of the GameObject to the physics body, set the transformObject property to the Transform component.
Note: The transformObject property is available only in a C# script, not 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 public PhysicsBodyDefinition object.
Make sure the type property of the physics body is set to PhysicsBody.BodyType.Dynamic.
For example, attach the following script to a GameObject, then enter Play mode. The physics body falls under gravity, and updates the position in the Transform component in the Inspector window.
using UnityEngine;
using Unity.U2D.Physics;
public class MoveGameObject : MonoBehaviour
{
public PhysicsWorld world;
public PhysicsWorldDefinition worldDefinition = PhysicsWorldDefinition.defaultDefinition;
void Awake()
{
// Enable physics bodies updating transforms
worldDefinition.transformWriteMode = PhysicsWorld.TransformWriteMode.Fast2D;
world = PhysicsWorld.Create(worldDefinition);
}
void Start()
{
// Create a body and shape
PhysicsBody circleBody = world.CreateBody();
CircleGeometry circleGeometry = new CircleGeometry { radius = 2f };
circleBody.CreateShape(circleGeometry);
// Set body to dynamic so it falls under gravity
circleBody.type = PhysicsBody.BodyType.Dynamic;
// Enable this physics body updating transforms
circleBody.transformWriteMode = PhysicsBody.TransformWriteMode.Current;
// Set the updated transform as the transform of this GameObject
circleBody.transformObject = transform;
}
}
By default, Unity sets a limit on how fast a physics body rotates, to avoid forces becoming too large and objects passing through each other incorrectly. To remove the limit, set the Fast Rotation Allowed property of the body to true. This property is recommended only for circular objects.
By default, the physics body updates the Transform component after Unity finishes calculating the physics simulation. To interpolate positions between simulation steps instead, set the TransformWriteMode of the physics body to Interpolate or Extrapolate.