written by
Marc Hewitt

Intro to Unity Physics - Part 4 Joints and Chain Physics

Unity3D 3 min read , May 12, 2021
Joints and Force help make Breakable panels like this

Unity3d has a few tutorials out there but one of my favorites is this official 2013 tutorial which is where the above gif is from. While the exact steps are a little different than what I’ve written below the base is still the same. To recreate the door we’d simply re-use the concepts below but just add a breakable force level to the joints.

Joints are great for breakable panels to rope physics, to bosses that can swing wrecking balls of death, to just Ragdolls.

The simple 101 examples of joint usage we’ll be doing are hanging platforms with interactable chains.

Simulating Chain Physics

Moving platforms are not always fun for players, but the fundamentals teach us all we need to know as coders

Not always the most useful application but setting up swinging platforms from a chain teaches all the basics of “joint” physics so it’s perfect for this article.

For this example, I’m using 3D models with 2D Physics chains as it’s a bit easier to demo and explain. It works similar to 3D but you have a few extra settings.

First, we set up our base distance and point, this is a 2D distance joint to simulate a weight hanging from something in the ceiling. But we could also simulate a monster pinned to a chain attached to a stake or to the wall with the same setup.

You just add the Rigidbody to “Connected Rigidbody” in Distance Joint

This is as easy as just saying what two objects we want to utilize. For the top point in my example we don’t want the ceiling to ever fall so we need to freeze its x/y and remove gravity, this gets our box hanging as if on a rope.

We “could” stop here and just say it’s magically hanging, but players require those visuals to believe it's working as intended.

Adding The Visual Chains

We don’t need to simulate per link, just every few

As in my first article on physics, the biggest thing to realize is a lot of physics simulation is “good-enough” work. We don’t require a simulated joint on every single length of chain, we just need enough joints so a player can suspend disbelief.

SCREENSHOT OF RigidBody and hinge

We can place Hinge Joint Anchors our selves if needed

The way we connect each chain is by attaching a RigidBody to make it a physics object, then a Hinge Joint. From here we grab the “next” chain’s Rigidbody as our hinge joint’s focus to essentially say “you are connected to this object”, then we move the “Anchor Point” to where we want to simulate the hinge.

You can see the “anchor” points as the blue dots and how they auto-config the sphere.

We repeat how many chains and joints we need, ending on the last object of our platform. And we apply an extra hinge to the top as well.

This creates believable enough hanging platforms.

Where to go from here

To stimulate more things, we could apply code or settings so when enough force or “weight” is applied it breaks the chain. Maybe we have a puzzle where enough physics objects need to get put on the platform to “break” it so it falls down revealing the exit.

If we put a breakpoint and just keep increasing the mass eventually it breaks. It’ll “Break” if we hit it with a hard object too

There’s so much you can do it just takes time to work it out, it all works off these base concepts.

unity3d tutorial programming