IK还是一个很神奇和实用的东西啊,起码可以用代码来控制人物骨骼位置还是很爽的。因为不是动画师~
这篇可能跟Animator没啥关系了哈,都是代码层的。
看了一官方的案例,老的,有些问题,自己修改了一下,达到了效果。
官方建议把所有的IK操作放在OnAnimatorIK中进行。
如果我们要进行IK操作,先要绑定IK位置。IK能控制的官方都给了enum
然后我们要设置权重Weight,范围为0~1,这里都设成1
avatar.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1.0f);
avatar.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1.0f);
avatar.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1.0f);
avatar.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1.0f);
avatar.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1.0f);
avatar.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1.0f);
avatar.SetIKPositionWeight(AvatarIKGoal.RightHand, 1.0f);
avatar.SetIKRotationWeight(AvatarIKGoal.RightHand, 1.0f);
avatar.SetLookAtWeight(1.0f, 0.3f, 1.0f, 0.5f);
如果我们把LeftFoot的权重设为0,则会出现以下情况
在官方给的脚本中IK的绑定并没有在OnAnimatorIK中,而放到了Update中,然后就出现了绑定失败的问题。
所以我把他们都放到了OnAnimatorIK中。就可以了。
关于IK的操作~,给的少。
//设置右手IK位置
avatar.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
//设置右手IK旋转
avatar.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
//获取右手IK位置
rightHandObj.position = avatar.GetIKPosition(AvatarIKGoal.RightHand);
//获取右手IK旋转
rightHandObj.rotation = avatar.GetIKRotation(AvatarIKGoal.RightHand);
上完整代码~ (ikActive)没开启时球跟随人,开启时人跟随球。
using UnityEngine;
using System.Collections; public class MyIK : MonoBehaviour { public Transform bodyObj = null;
public Transform leftFootObj = null;
public Transform rightFootObj = null;
public Transform leftHandObj = null;
public Transform rightHandObj = null;
public Transform lookAtObj = null;
private Animator avatar;
public bool ikActive = false; void Start()
{
avatar = GetComponent<Animator>();
} void OnAnimatorIK(int layerIndex)
{
if (avatar == null) return; if (ikActive)
{
avatar.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 0.0f);
avatar.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1.0f);
avatar.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1.0f);
avatar.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1.0f);
avatar.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1.0f);
avatar.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1.0f);
avatar.SetIKPositionWeight(AvatarIKGoal.RightHand, 1.0f);
avatar.SetIKRotationWeight(AvatarIKGoal.RightHand, 1.0f);
avatar.SetLookAtWeight(1.0f, 0.3f, 1.0f, 0.5f); if (bodyObj != null)
{
avatar.bodyPosition = bodyObj.position;
avatar.bodyRotation = bodyObj.rotation;
} if (leftHandObj != null)
{
avatar.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.position);
avatar.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.rotation);
}
if (rightFootObj != null)
{
avatar.SetIKPosition(AvatarIKGoal.RightFoot, rightFootObj.position);
avatar.SetIKRotation(AvatarIKGoal.RightFoot, rightFootObj.rotation);
}
if (leftFootObj != null)
{
avatar.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootObj.position);
avatar.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootObj.rotation);
}
if (rightHandObj != null)
{
avatar.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
avatar.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
} if (lookAtObj != null)
avatar.SetLookAtPosition(lookAtObj.position);
}
else
{
if (bodyObj != null)
{
bodyObj.position = avatar.bodyPosition;
bodyObj.rotation = avatar.bodyRotation;
} if (leftFootObj != null)
{
leftFootObj.position = avatar.GetIKPosition(AvatarIKGoal.LeftFoot);
leftFootObj.rotation = avatar.GetIKRotation(AvatarIKGoal.LeftFoot);
} if (rightFootObj != null)
{
rightFootObj.position = avatar.GetIKPosition(AvatarIKGoal.RightFoot);
rightFootObj.rotation = avatar.GetIKRotation(AvatarIKGoal.RightFoot);
} if (leftHandObj != null)
{
leftHandObj.position = avatar.GetIKPosition(AvatarIKGoal.LeftHand);
leftHandObj.rotation = avatar.GetIKRotation(AvatarIKGoal.LeftHand);
} if (rightHandObj != null)
{
rightHandObj.position = avatar.GetIKPosition(AvatarIKGoal.RightHand);
rightHandObj.rotation = avatar.GetIKRotation(AvatarIKGoal.RightHand);
} if (lookAtObj != null)
{
lookAtObj.position = avatar.bodyPosition + avatar.bodyRotation * new Vector3(, 0.5f, );
}
avatar.SetIKPositionWeight(AvatarIKGoal.LeftFoot, );
avatar.SetIKRotationWeight(AvatarIKGoal.LeftFoot, );
avatar.SetIKPositionWeight(AvatarIKGoal.LeftHand, );
avatar.SetIKRotationWeight(AvatarIKGoal.LeftHand, );
avatar.SetIKPositionWeight(AvatarIKGoal.RightFoot, );
avatar.SetIKRotationWeight(AvatarIKGoal.RightFoot, );
avatar.SetIKPositionWeight(AvatarIKGoal.RightHand, );
avatar.SetIKRotationWeight(AvatarIKGoal.RightHand, );
avatar.SetLookAtWeight(0.0f);
}
} }
附一个彩蛋,IK有意思~~呵呵~ o(* ̄▽ ̄*)ブ