"Attach" cloth.

Let's say I want to make a flag.

I make a stick, which is a cube with a long height. Then I make a cloth object. Technically, I want to somehow attach the cloth object to the cube object, simulating a flag... but I can't find a way to efficiently achieve such =/

What you need to do is to add a collider to the object you want to attach the cloth to. Make sure the cloth mesh and the collider overlap in some vertices. Then, in the cloth component, expand the "Attached Colliders" field, and set the size to 1 (or how man colliders you want to attach). Expand "Element 0" and Drag the collider object to the "Collider" slot.

Hook up the object as a child to the parent ( Drop the "flag cloth" object onto the pole object. An action you can perform in the editor tab named "hierarchy" when your scene and project are loaded.

What happens now is that when you change the pole, the same will happen to the "flag cloth". You can specifically add effects or other to child which will not effect the parent (like wind blowing through it) if you want for the future.

In Unity 5 this changed. Here is what I ended up doing to create my flag.

  • create an empty game object to hold the parts
  • Add a child cylinder as the pole
  • Add a child cloth as the flag
  • Add a mesh renderer to the cloth
  • configure the renderer to have two materials, one normal, one with inverted normals
  • click ‘Edit Constraints’
  • change to ‘Paint’ mode
  • select vertices on the mesh that you want constrained, for a flag, this is the full edge along the pole probably
  • configure some forces to make the flag flap

Images of the process above


Custom shaders I am using on the materials


Shader "Custom/NoBackfaceCulling" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		Cull Off
		
		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

Shader "Custom/InvertedNormals" {
	Properties {
	    _Color ("Main Color", Color) = (1,1,1,1)
	    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	    _BumpMap ("Normalmap", 2D) = "bump" {}
	    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
	}
	SubShader {
	    Tags {"IgnoreProjector"="True" "RenderType"="TransparentCutout"}
	    LOD 300
	    Cull Front
			
		CGPROGRAM
		#pragma surface surf Lambert alphatest:_Cutoff

		sampler2D _MainTex;
		sampler2D _BumpMap;
		float4 _Color;

		struct Input {
			float2 uv_MainTex;
    		float2 uv_BumpMap;
		};

		void surf (Input IN, inout SurfaceOutput o) {
		    half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
		    o.Albedo = c.rgb;
		    o.Alpha = c.a;
		    o.Normal = -UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
		}
		ENDCG
	} 
	FallBack "Diffuse"
}