Connect Physics Core 2D API objects with joints
Create a connection or constraint between two physics objects.
Read time 2 minutesLast updated 12 days ago
To create a connection or constraint between two Physics Core 2D API objects, create a joint that connects the two physics bodies.
For example, create a fixed joint that welds two bodies together, or a distance joint that keeps them a set distance apart. Use joints to simulate real-world mechanical behaviours like a spring or a hinge.
All joints allow you to set the anchor points on the bodies they connect, and limits on the force and torque they apply. For more information, refer to the API.
PhysicsJointFollow these steps:
-
Create twoobjects. For more information, refer to Create a physics object.
PhysicsBody -
Create a definition object for the type of joint you want. For example, aobject. Make it a
PhysicsDistanceJointDefinitionfield so it displays its properties in thepublicwindow.Inspector?
For example:public PhysicsDistanceJointDefinition jointDefinition = new PhysicsDistanceJointDefinition{ // Specify the two bodies to connect bodyA = body1, bodyB = body2, // Set the distance distance = 2f};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.For the full list of joint types, refer to.PhysicsJoint.JointType -
To create the joint, use themethod of your world object, and pass in the definition. For example:
CreateJointPhysicsJoint distanceJoint = world.CreateJoint(jointDefinition); -
If your script is in aclass attached to a GameObject, adjust the properties in the Inspector window.
MonoBehaviourTo configure the properties in your script instead, set the properties of the definition object before you create the joint. For more information, refer to Configure 2D physics properties using a definition.
If you destroy a body, any joints connected to that body are also destroyed. For more information, refer to Destroy physics objects and manage memory.
Example
The following example uses a fixed joint to create a fixed point that a large circle swings from.
using UnityEngine;using Unity.U2D.Physics; public class CreateJoint : MonoBehaviour{ public PhysicsDistanceJointDefinition jointDefinition = new PhysicsDistanceJointDefinition(); void Awake() { PhysicsWorld world = PhysicsWorld.defaultWorld; // Create a static fixed body PhysicsBodyDefinition bodyDefinition1 = new PhysicsBodyDefinition{ type = PhysicsBody.BodyType.Static, position = new Vector2(-5f, 0f), }; PhysicsBody object1 = world.CreateBody(bodyDefinition1); // Create a large circle below the fixed body PhysicsBodyDefinition bodyDefinition2 = new PhysicsBodyDefinition{ type = PhysicsBody.BodyType.Dynamic, position = new Vector2(5f, 0f), }; PhysicsBody object2 = world.CreateBody(bodyDefinition2); object2.CreateShape(new CircleGeometry { radius = 3f }); // Add the bodies to the joint definition jointDefinition.bodyA = object1; jointDefinition.bodyB = object2; // Add the joint to the world world.CreateJoint(jointDefinition); }}