Cloth Sprite

Web demo Get it on the Asset Store
Cloth Sprite is an easy to use 2D cloth for your Unity game. It's as simple to use as Unity's built-in sprite and is a perfect beckground decoration for your 2D game.


How does it work?

It creates an array of points and calculates their movement in relation to each other every frame using the Verlet integration. The computation is being run on CPU. Each point is affected by gravity, wind (if enabled) and pulling or pushing by neighboring points.

The object has a PolygonCollider2D wich is set to "trigger" mode to allow other 2D colliders pass through it. When the other collider enters it, Cloth Sprite script uses its location, speed and size to apply a force to the simulated points so the cloth moves when other objects move past it.

Cloth Sprite is intended to be used as a decoration in 2D games where other objects will move around and cause the fabric to move in background.

How to use

To create new Cloth Sprite object in your scene, click on GameObject menu in Unity window, then go to 2D Object and then click Cloth Sprite.

Now to assign a sprite to it, drag your sprite into the "Sprite" field of your newly created object.

Thet's it. If you hit play now, you'll see the cloth being simulated. Now you can configure its behavior and you can do it while the game is running too, to instantly see the result.


Sprite field holds your image that will be turned into cloth.

Color lets you tint your sprite same way as any regular Unity sprite.

Fixed points option lets you choose which points of cloth will be fixed (immovable) in space.

Resolution field defines how many such points will be created both horizontally and vertically. The cloth is a rectangular grid that consist of these points, connected to each other by constraint that keep them together.

Next you'll se a counter which indicates how many triangles the mesh has. It's not that important to the performance, it's just for curiosity, but the following two options are.

Point connections allows you to select from two ways to connect the simulated cloth points to each other. First way is just connecting every point to its neightbor on the left, right, top and bottom. The second way also adds the diagonal connection. The adventage of having diagonal connections is that the cloth will spread out more, be less saggy, but since each connection has to be calculated it doubles the number or those calculations.

Computation passes sets how many times each connection will be recalculated. Recalculating them multiple times makes cloth more firm. But as with previous option, it's a trade of. Together these two options are defining how many calculations will be performed each frame for this Cloth Sprite object.

Mass defines how much each simulated point of the cloth weights.

Stiffness defines how rigid those connections betweens points are. It can't make them perfectly rigid but it can make them more loose and relaxed. If you have diagonal connections enables you'll also see "Stiffness (diagonal)" that does the same for diagonal connections - these connections doesn't have to be that rigid. Generally you don't want to set regular stiffness to be less than diagonal - it stops looking like cloth.

Below this option you will see the status indicator. When cloth sprite is not moving for a few seconds it enters the "sleep" mode to stop calculating cloth physics.

Impact force is a number that multiplies the force, which is applied to cloth when another 2D colliders touches it. Cloth Sprite uses PolygonCollider2D in a trigger mode to detect any colliders 2D that enter it and move the cloth based on their size and speed. The impact only happens when something is entering Cloth Sprite's collider. So the "Impact force" lets you to make the power of that impact more subtle.

Wind checkbox allows you to ebnable constant wind for this cloth sprite. It enables two options below.

Wind directions sets the direction of the wind in degrees.

Wind force sets the power of the wind.

Sprite material lets you toggle between unlit and lit material. Both are based ond efault Unity sprite shaders.

Sorting layer and Order in layer fields work in the same way as they do for regular sprites.

That's pretty much it. If you'll have more questions you can always email me or contact me on Twitter. Feel free to get in touch if you encounter a bug or know how I can improve this asset.

Web demo Get it on the Asset Store

If you like this asset, please consider giving it a rating or a short review on the Asset Store. This will help others to discover it and make sure I'll keep improving it.