2012-12-25

[][]ラグドールテスト ラグドールテスト - Nao_uの日記 を含むブックマーク はてなブックマーク - ラグドールテスト - Nao_uの日記 ラグドールテスト - Nao_uの日記 のブックマークコメント

Unityでラグドールのテスト。

ついでにWiiリモコンでの射撃テスト、HDRと動的露出補正、SSAOなどで市街地をそれっぽく見せる調整の実験と、動画には撮ってないけどワイヤーによる立体機動アクションの検証も兼ねてる。

Tポーズにしてラグドールを埋め込んだモデルをPrefav化して、弾がヒットした時に、transform以下のすべての姿勢をコピーして、新しいモデルに切り替え。

姿勢生成時に、各関節に速度を追加。ヒットした点からの距離に応じて速度を減衰することで、ヒットした点から力が加わっているように見せる。

// ラグドールの生成 
private void GenerateRagdoll( Transform originalRoot, Vector3 velocity, Vector3 pos)
{
	GameObject ragdoll = (GameObject)Instantiate(mPrefavRagdoll);
	CopyTransformRecursively(originalRoot, ragdoll.transform, velocity, pos);
}

// トランスフォームを再帰的にコピーする 
// ついでに初速の設定も行う(力点との距離が離れたら減衰)
private void CopyTransformRecursively(Transform src, Transform dst, Vector3 velocity, Vector3 pos) 
{
	dst.localPosition = src.localPosition;
	dst.localRotation = src.localRotation;
	
	float maxDist = 0.4f; // 最大距離
	float dist = Vector3.Distance( dst.transform.position, pos );
	float pow = 0.0f;
	if( dist < maxDist ){
		dist /= maxDist;
		pow = Mathf.Pow( dist, 1.0f ); // 線形減衰でも問題なかった
	}
	if (dst.rigidbody) dst.rigidbody.velocity = velocity * pow;
	foreach(var child in src) {
		var srcChild = child as Transform;
		var dstChild = dst.Find(srcChild.name);
		if (dstChild) CopyTransformRecursively(srcChild, dstChild, velocity, pos);
	}
}

吹き飛ぶ前のポーズが両手を合わせた形になっているせいか、手と肩がおかしくなることがある。両手だけ広げる方向に初速を与える、とかで改善できる?