IK动画

大多数动画是通过将骨架中的关节角度旋转到预定值来产生的。子关节的位置根据其父关节的旋转而改变,因此可以根据其包含的各个关节的角度和相对位置来确定关节链的终点。这种构成骨架的方法称为正向运动学。
从相反的角度来看构造关节的任务通常是有用的 - 给定在空间中的选定位置,向后工作并找到定位关节的有效方式,以便终点落在该位置。当您希望角色在用户选择的点上触摸物体或在不平坦的表面上令人信服地放置脚时,这可能非常有用。这种方法被称为反向运动学(IK),并且在Mecanim中支持具有正确配置的头像的任何人形角色。
为角色设置IK,你通常在场景周围有角色与之交互的对象,然后通过脚本来设置IK,特别是动画师功能,如 SetIKPositionWeight, SetIKRotationWeight, SetIKPosition, SetIKRotation, SetLookAtPosition, bodyPosition, bodyRotation
创建一个Animator控制器,其中至少包含一个角色的动画。然后在Animator窗口的图层窗格中,单击图层的齿轮设置图标,然后在弹出的菜单中选中IK Pass复选框
确保Animator控制器分配给角色的动画师组件;

using UnityEngine;
using System;
using System.Collections;
[RequireComponent(typeof(Animator))] 
public class IKControl : MonoBehaviour {    
    protected Animator animator;    
    public bool ikActive = false;
    public Transform rightHandObj = null;
    public Transform lookObj = null;
    void Start () 
    {
        animator = GetComponent<Animator>();
    }    
    //a callback for calculating IK
    void OnAnimatorIK()
    {
        if(animator) {            
            //if the IK is active, set the position and rotation directly to the goal. 
            if(ikActive) {

                // Set the look target position, if one has been assigned
                if(lookObj != null) {
                    animator.SetLookAtWeight(1);
                    animator.SetLookAtPosition(lookObj.position);
                }    

                // Set the right hand target position and rotation, if one has been assigned
                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);
                }        
                
            }
            
            //if the IK is not active, set the position and rotation of the hand and head back to the original position
            else {          
                animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
                animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0); 
                animator.SetLookAtWeight(0);
            }
        }
    }    
}
由于我们不打算让角色的手伸到物体的中心(圆柱体的枢轴点),所以我们定位一个空的子物体(在这种情况下名为“圆筒抓取手柄”),手应该在圆柱体上,并相应地旋转它。手然后瞄准这个孩子对象。
抓取手柄”游戏对象应该被分配为IKControl脚本的“Right Hand Obj”属性
在这个例子中,我们将视觉目标设置为圆柱体本身,因此即使手柄靠近底部,字符也会直接朝向对象的中心。
进入播放模式,你应该看到IK复活。当你点击IKActive复选框时,观察字符的抓取和解开对象,然后尝试在播放模式中移动圆柱体,以查看手臂和手部追踪对象```。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值