When you started making this game, you called it Bobblehead Wars
but, so far, it’s been all wars and no bobble. Thankfully, Unity physics comes to the rescue with joints.
These aren’t the kinds of joints that would require you to be in Amsterdam, Oregon, Colorado or Washington, nor are they the kinds of joints made by the filmmaker Spike Lee.
These are physics joints with a more conventional purpose: Connecting two GameObjects together in a relationship defined by the physics engine.
Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.
Unity joint types
Think about a wrecking ball for a moment — specifically, the chain. Each link would be a GameObject, and you’d need to join them together with joints to allow each link to move individually but affect its neighbors and, in turn, the whole group is influenced by the ball.
There are many joints available to you:
- A hinge joint ties the movement of one GameObject to another — for instance, a door hinge.
- A spring joint acts like an invisible spring between two GameObjects.
- A fixed joint restricts a GameObject’s movement with an unrelated GameObject. A good example (from the documentation, even) is a sticky grenade.
- A character joint allows you to constrain motion to create rag doll effects.
- A configurable joint incorporates the features of the other joints and more. In essence, you use it to modify existing joints or create your own.
You’ll use a hinge joint to make the head bobble back and forth.
Playing off the example above, the space marine’s body is the door frame, and his head is the door. Every time the marine moves, you’ll add force to his head to make it bounce back and forth.
Adding joints
In the Hierarchy, expand SpaceMarine and select the BobbleMarine-Head. Then, from the main menu, click Component ▸ Physics ▸ Hinge Joint. As you can see, there’s more than one way to add a component.

Check the Inspector. You’ll see the hinge joint in there now.
For the hinge joint to work, the connected GameObjects must have Rigidbodies attached to them; Unity automatically adds these for you when you create a hinge joint.
In the Rigidbody component, uncheck the Use Gravity property.
In the Hierarchy, select BobbleMarine-Body and in the Inspector, click the Add Component button. From the component listings, select Physics and then Rigidbody. Click the preset button and select kinematic. Remember, this preset unchecks gravity and sets the Rigidibody to be Kinematic.

Everything is ready. Now you need to connect the two GameObjects.
Connecting the GameObjects to the hinge
Now, select BobbleMarine-Head in the Hierarchy, and look at the hinge joint in the Inspector. You’ll see many options!

The first option is the Connected Body, and, in this case, it’s the other GameObject that the head will be “hinged” to.
In the Hierarchy, drag the BobbleMarine-Body to the Connected Body property. Now, the GameObjects are connected by physics.

The Anchor indicates where the end point of the joint attaches to the GameObject as specified in the object’s local coordinate space. (0, 0, 0) within the head’s local coordinate space represents the neck area, so leaving the value at (0, 0, 0) is fine.
The Axis determines what direction the body will swing. You want this to move on the z-axis, i.e., up and down. Set the Axis to (0, 0, 1).
Configuring the anchor
The Connected Anchor parameter specifies the anchor point of the other end of the joint. If the Connected Rigidbody field is empty, this value is in the scene’s coordinate system. However, when Connected Rigidbody is set, as it is now to the space marine’s body, the Connected Anchor coordinates refer to the connected Rigidbody’s local coordinate space.
By default, Unity sets the Connected Anchor point to the same position as the Anchor point. For this game, you want these to be slightly different, so Uncheck the Auto Configure Connected Anchor checkbox.
Figuring out the connected anchor’s best settings tends to involve some trial and error. Thankfully, Unity shows you the anchor point’s location that you can use as a starting point.
Switch the Scene view to Wireframe mode to show a small orange line. This represents the anchor’s position, and it’ll update as you alter the Connect Anchor values.

You don’t need to stay in wireframe mode, so switch back to shaded draw mode. Back in the BobbleMarine-Head, set the Connected Anchor to (-2.06, 6.16, 0.16) to pin the anchor to the marine’s neck.
The spring properties will add “springy” behavior to the joint and the motor property will move the joint without any external force. To illustrate motor values, think of a door mysteriously opening on its own.
The Limits properties control how far the joint should swing. Check the Use Limits checkbox and expand the Limits property. In the Min property, put -25
and set the Max to 25
— this represents that the head should bounce at most 25 degrees in either way. To make it bounce like a proper bobble head, set Bounciness to 1
— that’s the max value.
Keep the rest of the values as they are, but take note of Break Force and Break Torque. You’d use these if you wanted the hinge to snap under a certain threshold, e.g., create the effect of a door being blown off its hinges.
Here’s the configured joint:

The hinge needs a collider to work. Click the Add Component button, and from the Physics category, select Sphere Collider. Set Center to (-0.09, 2.7, -0.21) and Radius to 2.36. This closely matches the width of the head.
Fixing the head
You have the bobble head all set up, but if you play the game and move, the head motion is a mess. It’s all over the place like it is glitching out. This is an issue with your setup. Rather, you are running into collision issues. The head is colliding with the space marine.
Select BobbleMarine-Head in the Hierarchy. In the Inspector, assign it the Head layer and select the Yes, change children option in the confirmation dialog.
Finally, click Edit ▸ Project Settings ▸ Physics, and in the physics matrix, uncheck the Player column in the Head row.

Now play the game. The head no longer goes crazy, but the bobble effect is subdued. Thankfully, you change this with a little code.
Writing the code
You’re going to add an increased bobble with a little code and a little force. Open PlayerController.cs in your code editor. You need a reference to the connected head Rigidbody, so add the following variable:
[SerializeField]
private Rigidbody head;
You might ask why you’re using a Rigidbody instead of a GameObject. Remember, a Rigidbody deals with physics. You’re adding the head to apply force to it, and there’s no way to apply force to a regular GameObject. Hence, you need a Rigidbody.
So how do you assign a Rigidbody to a public field? You’ll do this just as you did with the transform. Save your change and switch back to Unity.
Select the SpaceMarine in the Hierarchy. In the Inspector, look for a property named Head that can accept a Rigidbody. Drag the BobbleMarine-Head to it.

Much like it does with the transform, Unity uses the Rigidbody component attached to the GameObject to assign to the Head field.
Switch back to PlayerController.cs. Add the following underneath Update()
:
void FixedUpdate()
{
}
As you know, Update()
runs with every frame and it’s called as often as the frame changes, so 40 frames per second means it’s called 40 times per second.
FixedUpdate()
is an entirely different beast. Because it handles physics, it’s called at consistent intervals and not subject to frame rate. If you were to check the deltaTime
between each call to FixedUpdate()
, you’d see it doesn’t change.
Anything that affects a Rigidbody should be updated in FixedUpdate()
. Given that you are applying force to a Rigidbody to move the bobble head, you need to use FixedUpdate()
instead of Update()
.
Add the following to FixedUpdate()
:
Vector3 moveDirection = move.ReadValue<Vector2>();
Vector3 direction = new Vector3(moveDirection.x, 0, moveDirection.y);
Here you have a moveDirection
. You’ve done this before. Now add the following:
if (direction != Vector3.zero)
{
head.AddForce(((direction) * 150) * -1, ForceMode.Acceleration);
}
This code moves the head when the marine moves. First, you calculate the movement direction. If the value equals Vector3.zero
, then the marine is standing still. AddForce()
is how you get the head to move. You provide a direction then multiply it by the force amount.
There are different force types, and you’re using ForceMode.Acceleration
, which gives a continuous amount of force that ignores the mass.
Note: If you wanted to use the mass, you’d use
ForceMode.Force
. For more detail about the other options, search the documentation forForceMode
.
Now save your changes, play your game and move. Now have some Bobble action going on.
Where to go from here
Your bobble head marine now has some bobble going on. Right now, the bobble motion is pretty linear. You may want to play around with adding force such as when the marine takes damage. That’s beyond the scope of this tutorial series but do play around.
The final task at hand is to actually turn the marine to face the user’s mouse pointer. This will allow the marine to shoot in a complete circle. This also requires something known as a raycast. You’ll put this to work in the next tutorial.
Discover more from Jezner Blog
Subscribe to get the latest posts sent to your email.