Add a constraint
Connect two 2D Physics Core objects with a Physics Constraint component.
Read time 2 minutesLast updated 15 days ago
To connect two 2D Physics Core objects, add a Physics Constraint component to one of them, and assign the other as the object it connects to.
Follow these steps:
-
Select a GameObject with a Physics Pose component.
-
In thewindow, select Add Component.Inspector
?
-
Select Physics 2D (Core), then select a constraint component, for example Physics Constraint Distance.Unity connects one end of the constraint to this GameObject, by setting Pose A Source to the Physics Pose component. To connect a different GameObject instead, set Pose A Source to Custom.
-
Expand the Pose section, then drag the second GameObject you want to connect to the Pose B Target property, or select the picker (⊙).
For the full list of Physics Constraint components and what each one does, refer to Introduction to constraints.
Configure springiness
To make the constraint act like a spring, follow these steps:
- In the Inspector window, expand the Definition section, then expand Spring.
- Enable Enable Spring.
- Set Spring Frequency to a value greater than 0. A lower value makes the spring stiffer.
- Set Spring Damping to a value greater than 0. A higher value makes the spring settle more quickly.
Add a motor
To make the constraint act like a winch that pulls or pushes the connected objects, follow these steps:
- In the Inspector window, expand the Definition section, then expand Motor.
- Enable Enable Motor.
- Set Motor Speed to a value greater than 0 to make the motor push the connected objects apart, or less than 0 to make the motor pull the connected objects together.
- Set Max Motor Force or Max Motor Torque to a value greater than 0. These properties set the maximum force that the motor applies to reach the Motor Speed.
Share constraint properties
To share the same properties across multiple constraints, create a definition asset. For more information, refer to Share properties across multiple 2D Physics Core components.
To also allow the shapes on the two connected objects to collide with each other, enable Collide Connected in the Definition section of the constraint component.