Joints in Unity
Joints are commonly used components in Unity, and in today’s blog post, let’s talk about the different types of joint components in Unity and how they can be used in video games.
What exactly does a joint do?
Simply put, a joint is used to connect an object to another object or a fixed point in space. Joints apply forces that move objects and constraints to restrict that movement.
There are so many cool features that can be created with joints. For example, they can be found in gameplay features such as wrecking balls, sliding drawers, robotic arms, physics-related puzzles, machinery simulation, and more.
Different types of joints in Unity
Unity offers five different joint components, including: Character Joint, Configurable Joint, Fixed Joint, Hinge Joint, and Spring Joint. Each component applies different forces and limits to the Rigidbody component, and they can be used together to create complex features. You can read more about these joints in the Unity official documentation.
Demonstration
Overview
To demonstrate how joints work in Unity, let’s create a simple machinery simulation using a combination of different joints in Unity.
This simple-looking simulation actually uses three different joint components, including: Fixed Joint, Hinge Joint, and Configurable Joint. The mechanical arm, which is attached to a cube (1), can be broken down into four parts (2–5).
The cube is actually a separate GameObject from the arm. Here’s a screenshot of the Hierarchy window.
Connection between part 1 and part 2
In order for the cube to be attached to the arm, both parts need a Rigidbody component.
Also, the Is Kinematic property has to be unchecked so that forces or joints can take effect. Next, add a Fixed Joint component to part 1 and assign part 2 as the Connected Body. If you want the joint to break at a certain force, you can adjust the Break Force and Break Torque properties; otherwise, they can be set to Infinity.
Connection between part 2 and part 3
Similarly, both of these parts need a Rigidbody component with Is Kinematic unchecked. Next, add a Hinge Joint component to part 2.
After that, assign part 3 as the Connected Body, similar to what we did in the previous section.
If you click on the Edit Angular Limits icon, you can see where the anchor currently is.
As you can see, the anchor is not at the correct position, so to adjust its position, you can change the values of the Anchor property. I’m going with 0.1 on the y-axis.
And here’s the result:
Since I don’t want the joint to move freely 360 degrees, I’m going to enable the angular limit by checking the Limits property. Next, to adjust the limits, click on the Edit Angular Limits icon and adjust the handle in the Scene view.
Connection between part 3 and part 4
Next, to make part 3 move along its y-axis, add a Configurable Joint component and assign part 4 as the Connected Body.
To prevent part 3 from moving in other directions, lock the motion on the x and z axes and the angular motion on all axes.
To allow part 3 to move along its y-axis, adjust the position of the anchor. To do this, first uncheck Auto Configure Connected Anchor, and now you can manually adjust the y value of the Connected Anchor property; I’m going with -0.2.
To add a little bit of spring to the part, I’m gonna change the Spring value to 1.
Connection between part 4 and part 5
Similar to the connection between part 2 and part 3, add a Hinge Joint component to part 4, assign the Connected Body to part 5, and enable the Use Limits property.
Next, adjust the angular limits on this joint.
To make the arm attach to the wall, check the Is Kinematic property for part 5 so forces and joints don’t affect it.
If you follow all the steps mentioned, you should get the desired result.