Lesson 99 +10 XP

Cloth Physics

Cloth Physics

Unity's Cloth component simulates soft fabric physics on a SkinnedMeshRenderer. It is ideal for capes, flags, hair, and clothing that should react to wind, gravity, and collisions.

Adding Cloth to a Character

  1. Select a GameObject with a SkinnedMeshRenderer (typically a character's cape or skirt mesh).
  2. Add a Cloth component (Component > Physics > Cloth).
  3. The Cloth component automatically detects the SkinnedMeshRenderer on the same GameObject.

Cloth Constraints (Pinned Vertices)

Cloth simulates all vertices by default. You need to pin certain vertices so the cloth stays attached to the character:

  1. In the Cloth component Inspector, click Edit Cloth Constraints to enter the cloth editor.
  2. Select vertices at the top of the cape (near the shoulder attachment points).
  3. Set their Max Distance to 0 to pin them (they won't move).
  4. Leave the rest of the vertices free (set their Max Distance higher, e.g., 0.5).

Key Cloth Properties

PropertyDescription
Stretching StiffnessResistance to stretching (0-1). Higher = less stretchy.
Bending StiffnessResistance to bending. Higher = stiffer fabric.
DampingReduces oscillation. Higher = settles faster.
Use GravityWhether gravity affects the cloth.
External AccelerationConstant wind-like force applied every frame.
Random AccelerationRandom turbulence for realistic wind effect.

Cloth Colliders

Cloth does not collide with regular Unity colliders by default. You must add specific colliders to the Cloth's collider list:

// Add sphere colliders to the cloth (e.g., the character's body)
ClothSphereColliderPair[] pairs = new ClothSphereColliderPair[1];
SphereCollider bodyCollider = characterBody.GetComponent<SphereCollider>();
pairs[0] = new ClothSphereColliderPair(bodyCollider);
GetComponent<Cloth>().sphereColliders = pairs;

Scripting Cloth at Runtime

using UnityEngine;

public class WindEffect : MonoBehaviour
{
    [SerializeField] private Cloth _cloth;
    [SerializeField] private float _windStrength = 1f;

    void Update()
    {
        // Apply external acceleration to simulate wind
        _cloth.externalAcceleration = Vector3.right * _windStrength;
        _cloth.randomAcceleration = Vector3.right * (_windStrength * 0.5f);
    }
}

Performance Notes

  • Cloth is CPU-intensive. Limit cloth mesh complexity (keep vertex count low).
  • Only use Cloth for objects near the camera.
  • Disable the Cloth component when the character is off-screen.

TL;DR

  • Cloth simulates fabric physics on a SkinnedMeshRenderer.
  • Pin vertices with Max Distance = 0 to keep them attached to the character.
  • Configure Stretching Stiffness, Bending Stiffness, and Damping for fabric feel.
  • Manually add SphereColliders or CapsuleColliders to the Cloth's collider list.