Advertisement
  1. Game Development
  2. Unity 2D
Gamedevelopment

Unity 2D Joints: Slider, Relative, Spring, and Friction Joints

by
Difficulty:BeginnerLength:MediumLanguages:
This post is part of a series called Unity 2D Joints.
Unity 2D Joints: Distance, Hinge, Target, and Fixed Joints
Final product image
What You'll Be Creating

In the previous tutorial, we started looking at how 2D physics joints work in Unity and how to use them to achieve great effects (without sacrificing the game performance). In that tutorial, we covered the distance, hinge, target, and fixed joints.

Today, we'll continue by looking at the slider, relative, spring, and friction joints.

Slider Joint

This joint allows a game object controlled by rigid-body physics to slide along a line in space. The object can move anywhere along the line in response to collisions, forces, or by a motor force. Therefore, your next obstacle will be a moving platform that continuously moves between two fixed points.

Create a new empty GameObject, call it MovingPlatform, and set the Position of the object to (72.36, 1.57, 0). Next, you need to create three different platforms. Go to the Platforms folder and drag three sprites Platform1Platform6, and Platform12 into the MovingPlatform.

The Position of Platform1 should be (0, -1, 0), while for Platform6 it's (14, -1, 0), and for Platform12 (20.3, -1, 0). After placing the sprites, your scenario should look like this:

Slider Joint 2D - Initial positioning

Now that you have all the assets in place, it's time to create and configure the Slider Joint 2D. Select Platform6 and add a Slider Joint 2D component to it. Unity will automatically add a RigidBody 2D as well. Enable Freeze Rotation Z as well. Now, select Platform1 and add a RigidBody 2D. Enable the options Is Kinematic and Freeze Rotation Z.

Now, select Platform6, remove the Platform Effect 2D component, and disable the option Used By Effector on the Box Collider 2D component.

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 rigid body can be from the Connected Anchor point.

Turn Enable Collision on, since you want the platform to collide with the connected platforms. Drag Platform1 to the Connected Rigid Body. Set the Connected Anchor to (-0.082, -0.0646). Enable Use Motor and set the Motor Speed to -2.

If you press Play, you will see that the middle platform is now moving. However, once it has reached the nearest platform, it stops. To fix this, you will need to add the MovingPlatform script to Platform6. Drag Platform12 to Platform A and Platform1 to the field Platform B.

Slider Joint 2D - Script configuration

Press Play, and verify that everything is working as expected. Now that the obstacle is ready, create a prefab of it and place it in the Obstacles folder.

Relative Joint

Relative Joint 2D allows two game objects controlled by rigid-body physics to maintain a position based on each other’s location. To implement this joint, you will create two platforms. One is fixed in space, while the other will move around it, creating a circular obstacle.

Create a new empty GameObject at the Position (102.7, 4.2, 0) and name it Relative. Inside the Assets\GameAssets\Platforms folder, drag Platform11 into the Relative GameObject and change its name to Platform2. Change its Position to (-.45, -5.97, 0) and the Scale to (.5, .5, 1).  Add a Rigidbody 2D component and check the Is Kinematic property. The first part of the obstacle is created, so let's now proceed and add the second platform.

Add the MetalPlatform sprite (inside the Free Assets\Sprites folder) into the Relative GameObject, name it Platform, change the Order in Layer to 2, and set the Position to (-.37, 1.23, 0). Add two components: a Rigidbody 2D and a Box Collider 2D. Change the Size of the Box collider 2D to (2.32, 0.55).

Now add a Relative Joint 2D component to the Platform. Inside the Connected Rigid Body you should place Platform2.

The Max Force property sets the linear (straight line) offset between joined objects—a high value (of up to 1,000) uses high force to maintain the offset. The Max Torque sets the angular (rotation) movement between joined objects. Set both values to 200. The Auto Configure Offset should be checked.

If you run your game, you will see that nothing moves. You need to do a final step that involves adding the RelativeJoint (inside the Scripts folder) script into the Platform GameObject. Play the game and test if everything is working as intended.

The final joint configuration is:

Relative Joint 2D - Final configuration

Now that the obstacle is ready, create a prefab of it by placing the GameObject in the Obstacles folder.

Spring Joint

The Spring Joint 2D component allows two game objects controlled by rigid-body physics to be attached together as if by a spring. The spring will apply a specific force along its axis between the two objects, attempting to keep them a certain distance apart.

To use this joint, you will create a floating bridge composed by clouds; the clouds will be attached to another big cloud using the Spring Joint.

Start by creating an empty GameObject. Name it Spring and set its Position to (120, 10, 0). Add the Walkaway1 sprite (inside the Platform folder) into the Spring GameObject. Change its name to CloudSpring and the Order in Layer to 1. Add a Rigidbody 2D and check the Is Kinematic property.

Now, let's add four new sprites. Select the Platform9 sprite from the same folder and add four of them into the Spring GameObject. Name each object PlatSpring01 to PlatSpring04.

Each PlatSpring must be placed in the correct Position as follows:

  • PlatSpring01: -7.63, -12.35, 0

  • PlatSpring02: -2.67, -11.16, 0

  • PlatSpring03: 1.88, -12.44, 0

  • PlatSpring04: 6.77, -12.58, 0

Spring Joint 2D - Initial Positioning

Change the Order in Layer to 4 for all PlatSpring objects. Now, for each PlatSpring you need to add two components: a Rigidbody 2D and one Spring Joint 2D. For the Rigidbody 2D components, you only need to check the Freeze Rotation on the Z axis.

When you add the Spring Joint 2D, it creates by default a Connected Rigid Body to the Scene center.

Spring Joint 2D - Bad Connection Rigid Body illustration

That is not the intended effect. Therefore, you should attach the CloudSpring GameObject to the Connected Rigid Body of each PlatSpring.

Spring Joint 2D - Connection Rigid Body illustration

When you do that, the green line connection is automatically updated and placed inside the CloudSpring.

You can change the Connected Anchor and the Distance if you want. However, in this tutorial you will only change the Connected Anchor.

For each PlatSpring, change the Connected Anchor accordingly:

  • PlatSpring01-4.8, 0

  • PlatSpring02: -1.7, 0

  • PlatSpring031.07, -0.11

  • PlatSpring04: 4.13, -0.11

The complete configuration of PlatSpring01 is the following:

Spring Joint 2D - PlatSpring01 Configuration

Play your game and test if everything is well placed and configured. Now that the obstacle is ready, create a prefab of it by placing the GameObject in the Obstacles folder.

Friction Joint

The Friction Joint 2D connects objects controlled by rigid-body physics and reduces both the linear and angular velocities between the objects until they reach zero (i.e. it slows them down). To implement the Friction Joint 2D, you will create a moving platform that is connected to a cloud with physical properties.

Start by creating an empty GameObject. Name it FrictionPlatform and set its Position to (133, 5, 0). Add the Platform11 sprite (inside the Assets\Sprites folder) into the FrictionPlatform. Change its name to Support and set the Order in Layer to 1, its Position to (0, 0, 0), and its Scale to (.25, .25, 1). You should also add a Rigidbody 2D and check the Is Kinematic property. The remaining properties should be left at the defaults.

Now, add the MetalPlatform sprite into the FrictionPlatform. Change its Position to (.31, -8.33, 0), and the Order in Layer to 3. With the MetalPlatform selected, add a Rigidbody 2D, a Box Collider 2D, and a Platform Effector 2D. The Box Collider 2D Size should be adjusted to (2.35, 0.5) with Used By Effector on, while in the Rigidbody 2D you must check the Freeze Position Y and the Freeze Rotation Z. You should now have something like the following:

Friction Joint 2D - Positioning

If you play the game and jump over the MetalPlatform, you will notice that nothing moves. Let's change that by adding a Friction Joint 2D into the MetalPlatform. The Connected Rigid Body should be filled by the Support. For this joint, we will let Unity set the best anchor coordinate by enabling Auto Configure Connected. The Max Force and Max Torque should be 1, while the Break Force and Break Torque should be Infinity.

Run your game and let's test the friction joint. Now that the final obstacle is ready, create a prefab of it and place it in the Obstacles folder.

Conclusions

This concludes the second tutorial part about Unity Joints 2D. You learned about four joints, namely the slider, relative, spring, and friction joints. With this knowledge allied to everything you learned in the first part, you are now able to create complex 2D games using complex 2D physics properties. 

If you have any questions or comments, as always, feel free to drop a line in the comments.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.