So far, we have talked about rigid bodies and colliders, but they were just single physical bodies. What about more complex mechanical systems? Imagine a rope that is holding a cage with a skeleton and a key inside. So, our protagonist decides to cut the rope to make the cage fall. Once the rope is broken, then the cage falls down. However, even before the rope is cut, the cage was under the effect of gravity. In fact, if our protagonist would have pushed the cage instead of cutting the rope, that would have started to oscillate. The reason is that the rope is giving a constraint to the cage — it is allowed to move only within a circle (or sphere if we are in 3D) that has per radius the length of the rope. Of course, when it oscillates, gravity makes the cage move on the border of this circle (or sphere). What if the rope were a rigid metal bar? Well, the cage would even have a harder constraint, because now it is forced to be only on the border of the circle (or sphere). And if the rope was a spring? Again, this is another kind of constraint as shown in the following diagram:
Now, imagine placing many of these constraints together. For instance, you tie up the cage to a pending spring, and from the bottom of the cage a rope is holding a metal sphere. The system would start to increase in complexity. But how does Unity handle all of this? The answer is, using Joints components.
Note that all the joints that finish with 2D belong to the 2D Physics engine; otherwise they belong to the 3D one. So be careful to not get confused or attach the wrong joint. However, from now on, we will always refer to it as Joint 2D, even without 2D, unless otherwise specified. This decision was made to make the chapter clear and more fluid to read.
By using a joint component, you can attach a rigidbody to another one, in order to give them a specific constraint, and still leave some freedom for the motion. In particular, Unity provides nine Joint 2D components. But before we go through each one of them, let's explore some general properties of the joints in Unity.
As we've already said, a joint involves two rigidbodies (with the exception of the Target Joint 2D). The first is attached to the same object of the joint. The other one can be chosen arbitrarily. Therefore, these two options are available inside the joint components:
Then, most of the joints (with the exception for the Relative Joint 2D and the Target Joint 2D) needs the two points of the application of the joint. Imagine again the example of the cage pending from the rope. Where the rope is attached to the cage is an important factor, because if the rope is attached to one of the corners of the cage, this will be rotated, as shown in this diagram:
We attached the rope on the cage which is important, because it will affect how it will react to forces. However, it is interesting to note that wherever you append a rigidbody, the line along the rope will pass always through the barycenter (centroid) of the rigidbody.
Plus, you need to consider the possibility that there are other forces that may move the cage, and again where the rope is attached is an important piece of information. The same applies to the other rigidbody; in the case of the cage, the other rigid body is the ceiling.
Therefore, there are some options available to determine these two points of application, which are called anchors in Unity:
Coming back to the example of the suspended cage, imagine that the hero decides that it is a good idea try to jump on top of the cage and then jump on another platform. However, he didn't consider that the rope was really old (in fact, there is a skeleton in the cage!), and once he landed on the cage, the rope breaks making both the cage and our hero fall. Therefore, it's true that joints impose a constraint, but they have limits too. For the same reason, if you pull both sides of a rubber with too much force, the rubber will eventually break.
Also in Unity, Joints can be broken. In particular, in their components there are two variables:
For an example of a joint that breaks under torque, you can just imagine something that is rotating but it's motion is blocked by something, such as when you use the screwdriver and keep going until the wood (which was the constraint) is broken. However, here is another example that is less intuitive but more common (even in video games). Imagine you append yourself on the boarder/edge of a shelf. Since there is an offset from the center of mass of the shelf and the point where you have appended yourself (they are likely to be perpendicular if the shelf is flat), a torque is applied on the shelf. Under the effect of the torque, the shelf should rotate around its center of mass, but it doesn't since the wall prevents it. If you are heavy enough, which means the force you apply on the shelf is greater (or also if in some way you are able to increase the offset), the torque increases, until it reaches a breaking point. Then the constraint will break, which may result either in a broken shelf or a broken wall depending which one has more resistance (most probably the shelf will break and the wall will still stand). The following diagram should help you to figure the example out:
So far, we have seen only general properties of the joints in Unity, but now we will take a closer look at all the nine joint 2D components available.
It's worthy to mention that joints are divided into two classes: spring joints and motor joints. The former uses a spring to impose a constraint, which may be completely stiff so as to simulate a rigid bar. Motor joints, instead, can actively apply forces onto the rigidbodies. Some joints are both, like a spring with a motor.
All the joints in Unity have an icon (visible when, for instance, you place the component in the Inspector). In this book, you can see the icon in the top-left corner of the images of the joints (in the next sections). This icon is really useful for remembering what the joint does and also how it works. So, when reading the next section, or programming your game, pay attention to this icon, which might help you to understand the joint better.
This joint keeps the two rigidbodies at a certain distance apart. The aim of this joint is to keep at a certain distance two rigidbodies, or a rigidbody and a fixed point in the game world. In fact, if you set the Connected Rigidbody variable to None
, you can then specify the position of the fixed point in Connected Anchor.
It should appear like the following in the Inspector:
This joint applies a linear force to both the rigidbodies (or just to the rigidbody on which it is attached if using a fixed point) with a very stiff simulated spring (which is not configurable) to keep the distance. The joint doesn't apply any torque.
In addition to the parameters that we have seen in the last section, Distance Joint 2D also has the following options:
When Max Distance Only is false, the constraint among the two points is rigid, so you can imagine them connected by a metal string/bar along with a hinge (since they can still rotate with respect to each other). An example of how this is used is when you need to connect two couches of a train together, because they cannot get any farther or closer than a specific distance. If Max Distance Only is true, instead, they can be closer than the Distance, but not further. This is the behaviour of our rope, which holds the cage. that potentially can get closer to where the rope is tied but not further. Another example is a dog with a leash.
However, it is important to keep in mind that both the rigidbodies are free to rotate with respect to each other. In fact, this joint imposes the constraint only on the relative position of the two rigid bodies.
The aim of this joint is to keep a certain relative offset (both linear and angular) between two rigidbodies or a rigidbody and a fixed point in the game world (you can specify it by setting Connected Rigidbody to None
).
It should appear like the following in the Inspector:
This joints applies both a linear force to compensate the linear offset, and a torque to compensate the angular one. Like Distance Joint 2D, it uses a very stiff simulated spring, but you can tweak the spring values, such as the frequency.
Therefore, in addition to the parameters we have seen in the last section, Fixed Joint 2D has also the following options:
Another way to think of this joint is like parenting GameObjects within the Hierarchy so that the children are fixed with respect to their parents. However, the joint offers you more options than a simple parenting, including the possibility to break it.
An example of usage of this joint is when you have a chain of rigidbodies (such as a real chain that hangs from the ceiling, or you can imagine also a bridge made of sections), and you want to hold them rigidly together. The advantage is that you can allow a certain flexibility in the joint, so in the case of the bridge, it can still bend a little within the limits you impose.
The aim of this joint is to keep to zero both the linear and angular offset, by slowing down the movement between the two rigidbodies or a rigidbody and a fixed point in the game world (you can specify it by setting Connected Rigidbody to None
).
Friction Joint 2D should appear like this in the Inspector:
In addition to the parameters we have seen in the last section, Friction Joint 2D has the following options:
An example of the use of this joint is when there are physical objects in the game that need friction to be believable. So, imagine a platform that is anchored to a big wheel in the background. Since the game is 2D, the wheel is only an aesthetic element; it doesn't actually affect the platform. As such, we need to simulate the friction between the platform and the wheel, and we can achieve that with a Friction Joint 2D. In this way, we can have an angular resistance on the platform; thus it can still be rotated, but not so easily. Maybe, the player may drop a heavy box on the border of the platform to make it rotate enough so that the player can pass.
The aim of this joint is to constrain the rigidbody to rotate around another rigidbody or a fixed point in space (always specified by Connected Anchor if Connected Rigid Body is set to None
). The rotation can either be left to happen passively (for example, in response to a collision, or under the effect of gravity) or be actively powered by a motor, which provides torque to the rigidbody. Furthermore, limits can be set to constrain the hinge just to rotate at certain angles, or to allow more than a one full rotation around its axis.
It appears like the following screenshot in the Inspector:
In addition to the parameters that we have seen in the last section, Hinge Joint 2D has the following options:
A clear example of the usage of this joint is with doors. They can rotate around their hinges, which connect the door with its frame. We can limit how the door can rotate, so for instance, you may want it to rotate only of 90 degrees. Moreover, if the door is automatic, we can simulate a motor, which actively makes the door rotate. In fact, the motor may be triggered by a script. Finally, if the player wants to push the door beyond its limit (90 degrees in the example), and a Break Torque is specified, the player may break the hinge with enough force.
This joint makes two rigidbodies to keep a relative position based on each other's position. In fact, the aim of this joint is the same as that of the Fixed Joint 2D; the difference lies in how they do it. The Fixed Joint 2D, as we already have seen, is a spring kind of joint that will stop to oscillate only when the two rigidbodies are at the specified offset and rotation, and so the spring is in a rest position. Instead, the Relative Joint 2D is a motor kind of joint that applies a direct force and torque to rigidbodies so that they are at the same offset and rotation.
Like the Fixed Joint 2D, the Relative Joint 2D can also work with both of these:
To use the second case, set Connected Rigidbody to None
and then specify the coordinate of the fixed point in the Linear Offset variable.
It appears like this in the Inspector:
In addition to the parameters we saw in the last section, the Relative Joint 2D has the following options:
10000
, which means a really powerful motor.10000
, which means a really powerful motor.0.3
is usually appropriate (because on an average the joint behaves as you would expect; it's a value that has likely been found by trial and error). But it may need tweaking between the range of 0 and 1.An example of use of this joint is between the camera and the Avatar in the game. In this way, the camera can follow the Avatar with a slight delay (because if the camera is parented, it will move instantaneously with the Avatar). But it won't oscillate much as with the Fixed Joint 2D, since it is a spring kind, which may cause frustration to the player (of course, this also depends on how the Fixed Joint 2D is configured, maybe with a real stiff spring; but usually the Relative Joint 2D is used for this kind of situations).
Another typical example of the use of this joint is when something should follow the player, such as a life counter above her head or a friendly spirit behind her shoulders.
Imagine you want to constrain the motion of a rigidbody just along a line, so that it can only slide onto that line. This is what the Slider Joint 2D allows you to do. As for the other joints, the line can be between two rigidbodies or a rigidbody and a fixed point in the game world.
It appears like this in the Inspector:
In addition to the parameters we have seen in the last section, the Slider Joint 2D also has the following options:
In order to fully determine the line where motion is allowed by the joint, the current position the rigidbody is taken into consideration as well. Thus, a point (the current position of the rigidbody) and a direction (specified from the Angle variable) univocally determine a line where the motion is constrained, as shown in the following diagram:
Typical scenarios where you may want to use this joint are a sliding door, which goes up and down, and also a platform, which can go left and right, up, down, or even diagonally.
As the name suggests, this is a pure spring joint 2D. It actually simulates a spring connected between the two rigidbodies, or a rigidbody and a fixed point. In fact, this joint gives you all the functionalities for a spring joint, and therefore you can simulate all the other pure (without a motor) spring-joints.
It appears like the following in the Inspector:
In addition to the parameters we have seen in the last section, the Spring Joint 2D has the following options:
Whenever you need to place a spring in your game, this is the right joint. So, an example could be a physically realistic Launchpad, which you need to compress the spring, so as to let it have a boost in your jump. However, you don't need necessarily to think of this joint as a normal spring. In fact, you can bring this joint to the limits (such as a very stiff spring). As such, you are able to create other behaviours that in reality don't have a spring, but in the game they look nice when connected by a spring. Have you ever played Rayman? The character has its body parts separated, but they still move together in a coherent way in the game. If you are planning to create a similar character, the spring joint can be a valid solution to link the body parts in a realistic way. Here is an image of Rayman:
The Target Joint 2D is a particular spring-type joint that, instead of having a second rigidbody, has a target. The aim is to keep the rigidbody where the component is attached at a certain distance from the target. It only applies a linear force; thus no torque is given to the rigidbody.
It appears like this in the Inspector:
Since this component doesn't have a second rigidbody, there are some variables that allow you to specify the target:
Moreover, it is possible to control the characteristic of the spring with the usual parameters:
An example of the use of this joint is when the player needs to drag objects with the mouse. In this scenario, you can set the target of the object dragged to the cursor, so it will follow the cursor, without having a rigid movement (as if we would set frame after frame the position of the object to the mouse coordinates). Moreover, you can use the Anchor to specify where the object should be attached. For instance, if the player started to drag from a corner of the object, you can set the Anchor to be there, and as a result, the object will hang from that point.
The Wheel Joint 2D is a combination of a spring and motor kind of joint, and it has a very specific usage. As the name suggests, it simulates the constraint that a wheel can impose on a body. In particular, it can rotate the wheel with a motor (the wheel moves) and simulate suspensions with a spring.
More specifically, the joint applies a linear force to both connected rigid bodies to keep them on the line, an angular motor to rotate them on the line, and a spring to simulate the wheel suspension.
It appears like the following in the Inspector:
As you can see from the preceding screenshot, the parameters are divided into Suspension, which defines a spring, and a motor. Both use the same parameters we have already seen respectively for springs (with the exception of Angle) and motors.
The suspension is defined by the following:
90
degrees, which means the suspension will happen upwards, as usually happens for a car (the body of the car is on top of the wheels, so the direction of the suspension is along the positive y-axis, which means 90 degrees) as shown in the following diagram:In this diagram, we can see the the angle of the suspension for the wheel of the car is at 90 degrees, which means upwards in the game world. This is the default value. Imagine you want to have a car that is able to run on the ceiling thanks to magnetic wheels; you may consider changing the angle of the suspension. In the case of the ceiling, it is the opposite of the normal situation, so the angle will be 270 degrees.
The motor is defined by:
Needless to tell you which is the most common usage of this joint. Every time in your game you have a wheel and it needs to move in a realistic manner, this joint is the deal for you.
3.147.76.89