IK还是一个很神奇和实用的东西啊,起码可以用代码来控制人物骨骼位置还是很爽的。因为不是动画师~
这篇可能跟Animator没啥关系了哈,都是代码层的。
看了一官方的案例,老的,有些问题,自己修改了一下,达到了效果。
官方建议把所有的IK操作放在OnAnimatorIK中进行。
如果我们要进行IK操作,先要绑定IK位置。IK能控制的官方都给了enum
然后我们要设置权重Weight,范围为0~1,这里都设成1
1 avatar.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1.0f);2 avatar.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1.0f);3 avatar.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1.0f);4 avatar.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1.0f);5 avatar.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1.0f);6 avatar.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1.0f);7 avatar.SetIKPositionWeight(AvatarIKGoal.RightHand, 1.0f);8 avatar.SetIKRotationWeight(AvatarIKGoal.RightHand, 1.0f);9 avatar.SetLookAtWeight(1.0f, 0.3f, 1.0f, 0.5f);
如果我们把LeftFoot的权重设为0,则会出现以下情况
在官方给的脚本中IK的绑定并没有在OnAnimatorIK中,而放到了Update中,然后就出现了绑定失败的问题。
所以我把他们都放到了OnAnimatorIK中。就可以了。
关于IK的操作~,给的少。
1 //设置右手IK位置2 avatar.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);3 //设置右手IK旋转4 avatar.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);5 //获取右手IK位置6 rightHandObj.position = avatar.GetIKPosition(AvatarIKGoal.RightHand);7 //获取右手IK旋转8 rightHandObj.rotation = avatar.GetIKRotation(AvatarIKGoal.RightHand);
上完整代码~ (ikActive)没开启时球跟随人,开启时人跟随球。
1 using UnityEngine; 2 using System.Collections; 3 4 public class MyIK : MonoBehaviour { 5 6 public Transform bodyObj = null; 7 public Transform leftFootObj = null; 8 public Transform rightFootObj = null; 9 public Transform leftHandObj = null; 10 public Transform rightHandObj = null; 11 public Transform lookAtObj = null; 12 private Animator avatar; 13 public bool ikActive = false; 14 15 void Start() 16 { 17 avatar = GetComponent(); 18 } 19 20 void OnAnimatorIK(int layerIndex) 21 { 22 if (avatar == null) return; 23 24 if (ikActive) 25 { 26 avatar.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 0.0f); 27 avatar.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1.0f); 28 avatar.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1.0f); 29 avatar.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1.0f); 30 avatar.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1.0f); 31 avatar.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1.0f); 32 avatar.SetIKPositionWeight(AvatarIKGoal.RightHand, 1.0f); 33 avatar.SetIKRotationWeight(AvatarIKGoal.RightHand, 1.0f); 34 avatar.SetLookAtWeight(1.0f, 0.3f, 1.0f, 0.5f); 35 36 if (bodyObj != null) 37 { 38 avatar.bodyPosition = bodyObj.position; 39 avatar.bodyRotation = bodyObj.rotation; 40 } 41 42 if (leftHandObj != null) 43 { 44 avatar.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.position); 45 avatar.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.rotation); 46 } 47 if (rightFootObj != null) 48 { 49 avatar.SetIKPosition(AvatarIKGoal.RightFoot, rightFootObj.position); 50 avatar.SetIKRotation(AvatarIKGoal.RightFoot, rightFootObj.rotation); 51 } 52 if (leftFootObj != null) 53 { 54 avatar.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootObj.position); 55 avatar.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootObj.rotation); 56 } 57 if (rightHandObj != null) 58 { 59 avatar.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position); 60 avatar.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation); 61 } 62 63 if (lookAtObj != null) 64 avatar.SetLookAtPosition(lookAtObj.position); 65 } 66 else 67 { 68 if (bodyObj != null) 69 { 70 bodyObj.position = avatar.bodyPosition; 71 bodyObj.rotation = avatar.bodyRotation; 72 } 73 74 if (leftFootObj != null) 75 { 76 leftFootObj.position = avatar.GetIKPosition(AvatarIKGoal.LeftFoot); 77 leftFootObj.rotation = avatar.GetIKRotation(AvatarIKGoal.LeftFoot); 78 } 79 80 if (rightFootObj != null) 81 { 82 rightFootObj.position = avatar.GetIKPosition(AvatarIKGoal.RightFoot); 83 rightFootObj.rotation = avatar.GetIKRotation(AvatarIKGoal.RightFoot); 84 } 85 86 if (leftHandObj != null) 87 { 88 leftHandObj.position = avatar.GetIKPosition(AvatarIKGoal.LeftHand); 89 leftHandObj.rotation = avatar.GetIKRotation(AvatarIKGoal.LeftHand); 90 } 91 92 if (rightHandObj != null) 93 { 94 rightHandObj.position = avatar.GetIKPosition(AvatarIKGoal.RightHand); 95 rightHandObj.rotation = avatar.GetIKRotation(AvatarIKGoal.RightHand); 96 } 97 98 if (lookAtObj != null) 99 {100 lookAtObj.position = avatar.bodyPosition + avatar.bodyRotation * new Vector3(0, 0.5f, 1);101 }102 avatar.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 0);103 avatar.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 0);104 avatar.SetIKPositionWeight(AvatarIKGoal.LeftHand, 0);105 avatar.SetIKRotationWeight(AvatarIKGoal.LeftHand, 0);106 avatar.SetIKPositionWeight(AvatarIKGoal.RightFoot, 0);107 avatar.SetIKRotationWeight(AvatarIKGoal.RightFoot, 0);108 avatar.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);109 avatar.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);110 avatar.SetLookAtWeight(0.0f);111 }112 }113 114 }
附一个彩蛋,IK有意思~~呵呵~ o(* ̄▽ ̄*)ブ