unity IK反向动力学

不知道你是否仔细观察过在游戏中人物站立在台阶上的动作?

为保证人物移动时平稳,楼梯实际上往往都会是一个大斜坡,然后用材质渲染出楼梯的效果。

然而人物总是双脚开立站在楼梯上,而在移动时偶尔会发生细微的穿模,如何解决站立在斜坡上不穿模问题,就能够用到IK,通过IK对人物动画进行细微调整。

请确保人物有动画Animator,以及骨骼正确,动画能够正确播放。

 首先来到人物动画这里,勾选IK Pass开启IK。

 代码如下:

必须先获取到Animator


	public class IKCtrlRightHand : MonoBehaviour
	{

		private Animator anim;
		public Transform targetObj = null;
		public bool isIkActive = false;
		public float mixWeight = 1.0f;

		void Awake ()
		{
            //获取动画
			anim = GetComponent<Animator> ();
		}

		void Update ()
		{
			
		}

		void OnAnimatorIK (int layerIndex)
		{
            //判断是否开启
			if (isIkActive) {
                //设置人物 身体组件权重  右手  权重1(权重设置范围0-1,0关闭1开启)
                //分别是坐标位置 和旋转角度
				anim.SetIKPositionWeight (AvatarIKGoal.RightHand, mixWeight);
				anim.SetIKRotationWeight (AvatarIKGoal.RightHand, mixWeight); 
                //设置目标位置   右手  目标坐标位置
				anim.SetIKPosition (AvatarIKGoal.RightHand, targetObj.position);
				anim.SetIKRotation (AvatarIKGoal.RightHand, targetObj.rotation);
			}
		}

		
	}

然后IK系统会通过目标位置自动计算,使人物合理,值得注意的是,SetIKPositionWeight和SetIKRotationWeight要同时使用才能达到良好的效果。

猜你喜欢

转载自blog.csdn.net/qq_46043095/article/details/130599827