imagination XD

realizing dream

Joint-based Hair / Cloth Physics for Unity

12 Comments

I was doing the MMD4Unity research. I do this because, on Google, if you want a Hair Physics, you have to wrote a script, or key frame it naturally in your model. Ain’t nobody got time for that keyframing! So, this might be very useful for easy Joint-based hair / cloth physics, even wrote a script was useful too.

Okay, so it’ll be like this. It could be very awesomely epic if you are not lazy to set the character joint setting.

“It’s long and complicated, i better use keyframe method.” Yeah true, but what to do when the hair colliding object because of your keyframing method ?

Let’s get Neru Akita as example (Model from MMD, some motion from Unity-chan).

Unity 2014-07-27 14-07-06-60_2

1. Now Select cloth / hair bones and then add Capsule Collider like the picture below.
1(bones)

2. After that, adjust the size of the colliders and then add Character Joint.
2(bones)

3. If your character has a Character Controller on it / Playable, Open Tags & Layer, add 2 layers. Set ‘Player’ layer to object that has Character Controller on it. Else, set the object that you want to have a hair / cloth physics like Hair Bones1, Hair Bones2, and so on, or Hair/Cloth physics collider like MidSpine (That has Collider, so hair / cloth wont clip like hell). Basically, Just set Cloth Layer to Skinned Mesh Renderer, and set it to all children.
3b (Bones)

4. Go to your character controller script. Whatever script is OK as long as its located on Object that has Character Controller on it. Don’t make a new script. Just add this line to function Start() or void Start(). Works on both C# and JS. 8 is Cloth layer, 9 is Player layer. it’s all up to you on what layer you set the Cloth or Player layer. If you set cloth layer to 10, then write 10.

Physics.IgnoreLayerCollision(8,9);

3c (bones)
5. Alright now let’s add Rigidbody on cloth’s / hair’s root bone. For the root of the cloth (like this below), turn on isKinematic and turn off useGravity. Root doesn’t need a Character Joint, Collider is optional.

4 (bones)
6. Now set the Character Joint’s connected body. For instance, 1 (Cloth Root), 2 (1’s child), 3 (2’s child). 3’s connected body is 2, 2’s connected body is 1, and 1 doesn’t have a Character Joint. Like the picture below.
5 (bones)
7. Set bone’s swing limit, spring, high twist limit, low twist limit that suits to your model.
6 (bones)
8. Now add some collider to body that Hair or cloth will collide. Remember to set the body layer to CLOTH, so your character controller wont collide it. I add the colliders on Body, Hands, and Head.
7 (bones)

Because if you don’t add colliders, it’ll look like this:
Unity 2014-07-27 14-07-06-60

Now look! A cloths dynamic! It much better than the method I use on Wild West Showdown (Skinned Cloth). Look at her front hair and tie.
Unity 2014-07-27 14-07-06-60_1

 

As I said, I leave making games because I need to level up my CG level. I said I was give up right ? No, and never. I just need to learn to make a better CG Artwork. I’m currently making a mascot model for Imagination, named Imajine-chan. but, I don’t have an inspiration for her character design.

EDIT : “My Character raycast’s script is broken because of this.” Well, you could replace Cloth to IgnoreRaycast layer.

Author: IMGVERTEX

Started making games since 13 year old. 3D CGI Weeaboo | VFX Otaku | Anime Researcher | Indie Game Dev | VR Chuunibyou

12 thoughts on “Joint-based Hair / Cloth Physics for Unity

  1. Nice tutorial of jigglebone! :P
    for me, i dont use collider and i use detached bone from parent at start(). Looks ok but i think yours is more better

  2. Hi!
    Can I ask how would you do a long flowing skirt or a robe? I know Unity has two options for it (SkinnedCloth and DynamicCloth) but the documentation is a bit too generalistic.

    • Well, you have to make a long and a lot of joints in every segments.

      Like, Skirt1 > Skirt1.1 > Skirt1.2 > Skirt1.3 > and so on.

      I don’t really want to use skinnedcloth, because it’s not really good / laggy sometimes.

  3. Is it supposed to work with any character? because i’m trying to do it but the hair won’t follow the character dynamically, it moves with it but not simulates physics.

    • Yeah it works on any character. You probably didn’t make some collider in your character. Or maybe your collider was ignored by your hair rigidbody, check your rigidbody ignore collision

  4. Hi,

    I am working on cloth simulation on a human model and using Unity. My issue is since the new Cloth has only sphere, cone and capsule colliders, it is difficult for me to add it dynamically to the body. Can you suggest a way to add mesh collider on the body more or less close to its shape. As I am more focussed on fitting of cloth on the body, sphere colliders won’t work. Will I need Unity shaders for this?

    Thanks.

    • I’m not sure, but the point of putting those primitive colliders are so it doesn’t slow down performance much.
      And also you can put those on every joints, which Mesh collider wont.

      If you prefer use Mesh collider, just use cloth renderer, it’s kinda heavy, but yeah

  5. Hi, thanks for this tutorial! I tried for my character and it works great for the ponytail. But I cant make it work for the fornt hairs, no matter how much i adjust the settings the fornt hair doesnt move smoothly, any help would be appreciated!

  6. IM SO CONFUSED D: IM JUST TRYING TO MAKE THE CAPES ON MY MODEL MOVE

  7. Hello, I’m trying this using UnityChan model as she has similar hair, and using Ethan characters controller setup.

    At step 4, adding the Physics.IgnoreLayerCollision(9,10); to my controller script, that character goes into crouch mode and refuses to move. By commenting out this line, she is controllable again?

    • i also have the same problem whenever i use Ethan characters controller setup.

      i think the root cause of this ‘Forced Crouch’ is somehow when 2 or more collider collide at each other (one in yout hair, and the other on the hair) …. void PreventStandingInLowHeadroom() in ThirdPersonCharacter.cs got activated

      if we look closely at the void

      void PreventStandingInLowHeadroom()
      {
      // prevent standing up in crouch-only zones
      if (!m_Crouching)
      {
      Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
      float crouchRayLength = m_CapsuleHeight – m_Capsule.radius * k_Half;
      if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
      {
      m_Crouching = true;
      }
      }
      }

      i think the Raycasting completely ignore your Physics.IgnoreLayerCollision(9,10)

      So… i think we have to use another characters controller setup, or modify the Physics.AllLayers in ThirdPersonCharacter.cs

FEED ME