Physics Joints in Unity 2D

In this tutorial, you will learn about various physics joints available to you in Unity 2D. By Pedro Pereira & Orlando Pereira.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Slider Joint 2D

Slider joints restrict an object’s movement along a line in space. In this section, you will use the hinge joint you just created to start another object moving along a slider joint.

Select Hexagon_Sljoint in the Hierarchy and add a Slider Joint 2D component to it:

27

The fields available in the Slider Joint 2D component are similar to those you saw in the hinge joint, but with a few semantic changes. Because slider joints deal in linear, rather than angular, motion, Motor Speed is measured in units per second. Likewise, instead of angle limits, you can specify translation limits. Translation limits work the same way as angle limits, except they specify the distance the Rigidbody can be from the Connected Anchor point:

28

Leave Connected Rigidbody empty and set Connected Anchor‘s X value to 4.3 and its Y value to -4.7:

29

You can see the path described by the joint in the Scene view as shown below:

slider_joint_scene

The one completely new field you see is Angle; this sets the angle between the Anchor point and the Connected Anchor point. The GameObject with the slider joint will reposition itself at runtime to match this angle.

Set the Angle to -45. You’ll notice a small green line in the scene view will replicate that angle.

Screen Shot 2015-05-25 at 2.29.55 PM

Now, run your game. You’ll see that instead the joint is now positioned at a forty five degree angle.

Screen Shot 2015-05-25 at 2.31.57 PM

Since you want Hexagon_Sljoint to move to the right of where it starts in the scene, set the Angle back to 0.

In order to see the joint’s path at runtime, add the Line script to Hexagon_Sljoint, set its Game Object 1 field to Hexagon_Sljoint and its Game Object 2 field to Square:

30

Play your scene; drop some balls on the hinged platform from the previous section. The platform should spin and bump into Hexagon_Sljoint, which will then slide along the path defined by the joint. The hexagon will move at different speeds depending on how fast you make the platform spin:

31

While you can make rotating objects with hinge joints, you’ll want to use a wheel joint to simulate things such as the wheels on a car.

Wheel Joint 2D

Wheel Joint 2d is the final joint you’ll explore in this tutorial; it simulates a rolling wheel you can connect to another object, complete with a suspension spring to provide some “give” between the wheel and its other anchor point.

This joint exists primarily to simulate the wheels of vehicles. However, in this example you’ll keep it simple and just use it to create a rotating platform.

Add a Wheel Joint 2D component to the Square_WJoint:

32

Most of this component’s fields should look familiar. One difference is that you shouldn’t leave Connected Rigid Body unset; Unity won’t complain if you do, but the wheel joint won’t do anything in that case.

33

To anchor your platform in the scene, set Connected Rigid Body to Hexagon_WJoint:

34

The Suspension section is unique to wheel joints. It includes three fields: Damping Ratio, Frequency and Angle. The first two work just like they did in Spring Joint 2D, while Angle works as it did in Slider Joint 2D.

These three properties work together to describe a spring that keeps the wheel separate from its anchor vehicle and oriented in a specific direction.

Play the scene; drop one of the balls over Square_Wjoint, near the center of the platform. You should see the platform move down and bounce back up along with its spinning motion — the end result of the Suspension settings:

35

To see the Wheel Joint really in action, create a new Scene by selecting File \ New Scene.

From the Sprites folder, drag a square into the Scene view. Set the Transform position to: (-3.6, -7.72, 0). Set the Scale to (14.43, 6.50, 1).

Click the Add Component button and give it a Box Collider 2D component. Set the Size to X: 2.29, Y: 2.31

Screen Shot 2015-05-25 at 5.37.16 PM

Give it the name: Ground. It should look like the following:

Screen Shot 2015-05-25 at 4.14.17 PM

Create a new Empty GameObject. Call it Car. Set the position to: (6.4,1.13,0).

Screen Shot 2015-05-25 at 4.32.42 PM

From the Sprite folder, drag the Car sprite from the Sprites folder and into the Car GameObject. Give it the name: Body. Set the transform to be: (0, 0, 0)

Screen Shot 2015-05-25 at 4.28.40 PM

From the Sprite folder, drag the Wheel sprite from the Sprites folder and into the Car GameObject.Do the same for both the wheels, positioning them in the empty wheel wells. Call them: FrontWheel and BackWheel.

Screen Shot 2015-05-25 at 4.33.45 PM

Next, add Rigidbody 2D components on the Body, Front Wheel, and BackWheel. With your Rigidbodies in place, add a Circle Collider 2D to the FrontWheel and BackWheel. Add a Polygon Collider 2D to the Body.

When you select the Car GameObject, your colliders will look like the following:

Screen Shot 2015-05-25 at 4.39.24 PM

Next, select the Body in the Inspector, and click the Edit Collider button.

Screen Shot 2015-05-25 at 4.46.16 PM

Drag the polygon collider around the wheel wells so that the wheels won’t collider with them.

car-collider

Select the Body in the Inspector. Add a Wheel Joint 2D to it. In the Connected Rigid Body property, add the FrontWheel. In the Anchor property, set X:-0.98 Y:-0.78. Check the Use Motor checkbox and set the Motor Speed to 500.

Screen Shot 2015-05-25 at 5.27.59 PM

With the Body still selected. Add another Wheel Joint 2D to it. In the Connected Rigid Body property, add the BackWheel. In the Anchor property, set X:1.15 Y:-0.84. Check the Use Motor checkbox and set the Motor Speed to 500.

Screen Shot 2015-05-25 at 5.30.09 PM

With your engine ready to go, play your scene. You’ll see your car take a ride!

car-driving

Where to Go From Here?

You can download a copy of the completed project here.

We hope you enjoyed this tutorial about 2D joints! If you’d like to learn more about Unity’s 2D joints, we suggest you check out the following resources:

If you have any comments or questions on this tutorial, feel free to join the discussion in the forums below!

Pedro Pereira & Orlando Pereira

Contributors

Pedro Pereira & Orlando Pereira

Author

Over 300 content creators. Join our team.