IK
IK是什么?
IK是反向运动学:
设置IK
创建 Animator Controller,使其包含该角色的至少一个动画。然后,在 Animator 窗口的 Layers 面板中,单击层的齿轮设置图标,并选中弹出框中的 IK Pass 复选框。
将Animation Controller分配给Animator组件
设置脚本
脚本名为ikControl,脚本为角色的右手设置 IK 目标。
using UnityEngine;
using System;
using System.Collections;
public class iKControl : MonoBehaviour {
public Animator animator;
public bool isActive;
public Transform rightHand;
public Transform leftHand;
void Start(){
animator=GetComponent<Animator>();
}
void OnAnimator(){
if(animator){
if(isActive){
// 设置观察目标位置(如果已分配)
if(lookObj != null) {
animator.SetLookAtWeight(1);
animator.SetLookAtPosition(lookObj.position);
}
// 设置右手目标位置和旋转(如果已分配)
if(rightHandObj != null) {
animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
}
}
// 如果 IK 未处于活动状态,请将手和头部的位置和旋转设置回原始位置
else {
animator.SetIKPositionWeight(AvatarIKGoal.rightHand,1);
animator.SetIKPositionWeight(AvatarIKGoal.lefttHand,1);
animator.SetLookAtWeight(0);
}
}
}
}
调整手的位置,因此在物体下建立一个空物体以确保手在物体上,手的目标是空物体。