How to easily implement Hair and Clothes Physics in Unity

Published

Dealing with 3D (and 2D models) is fascinating, and I’ve come to realize that it is the small details that truly breathe life into a subject. In this article, I’ll share with you an easy way to set up hair and clothes physics. No code required!

Getting the Spring Bones Component

In a 3D model, hair and clothes usually contain bones to be used for animation. What we want for our simulation is for our hair bones to move when the model itself moves, and then slowly go back to their initial position: this is called a Spring Bone.

There are some (paying) assets available for this on the asset store, which I recommend checking out for more complex projects. But for our need we have something good: code from the Unity itself, specifically the Unity Japan division.

Unity Japan has several repositories available on Github, but one of them, the Unity-chan toon shader repository contains code for Spring Bones! That’s great!

Go to this page, and download the following files:

  • SpringBone.cs
  • SpringCollider.cs
  • SpringManager.cs

Put them in your project and we’re ready to go!

Setting the Spring Bones on our Model

The process is simple:

  • In the main object of your model, add a Spring Manager Component
  • Go to the hair section of your model
  • For each hair strand, and each bone composing it, add a SpringBone component
  • Set bone axis as (0, 1, 0), and add the transform of the child bone of the hair strand
  • Register all the Spring Bones in the Spring Manager
Spring Bones Settings
Spring Bones Settings

The Spring Manager will apply its stiffness and drag parameters to all bones, but this can be overriden by toggling the “Is Use Each Bone Force” parameter if needed. Don’t forget to set the bone axis to (0, 1, 0) else your hair will be dragged to weird directions!

Spring Manager Settings
Spring Manager Settings

Play around with the settings until you get something that fits! Here’s a small demonstration using the parameters above:

Showing off the Hair Physics
Showing off the hair physics

Easier Setup using my Utility Script

The setup is a bit boring and painful (lots of clicking and dragging), so I created a small script to help out: the Script explanation and link to Github are available here.

Prevent Hair clipping through the Model with SpringColliders

As you might have noticed on the gif before, the hair strands clip through the mesh. Thankfully, we can solve it: among the scripts you downloaded before, there is the SpringCollider component. It basically creates a “sphere” where the spring bones cannot reach. Set them up on your mesh as appropriate by adjusting their position and size to prevent the hair from clipping through the clothes. Finally, you’ll have to add the colliders as reference for all the Spring Bones components.

With this you have successfully added Hair Physics to your model, congratulations! These Spring Bones can also be used for clothes physics as well.

So far, the performance looks okay, I might make an article about performance checks for these scripts and share improved scripts as well. Once again, check out my other article for faster setup of Hair physics in Unity.