unity新动画系统之IK动画

本文详细介绍如何在Unity中使用IK(反向动力学)动画,通过代码实现角色头部、左脚和左手的动态调整,使角色能够对场景变化做出自然反应。

国际惯例,先来一段说明。IK动画全称Inverse Kinematics,即反向动力学,牵一发而动全身的既视感。
代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IK_test : MonoBehaviour {

    Animator _animator;
    public Transform sphereHead,sphereLeftFoot,sphereLeftHand;
    bool isIK = true;
    // Use this for initialization
    void Start ()
    {
        _animator = transform.GetComponent<Animator>();
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            isIK = false;
        }
    }
    //启动IK动画的unity特定方法OnAnimatorIK(),IK Pass要勾选才行
    private void OnAnimatorIK()
    {
        if (isIK)
        {
            //设置头部转向的权重,取值范围0-1,转动的幅度与数值呈正比(只有头部转动哦)
            // _animator.SetLookAtWeight(0.2f);
            //头部带动身体转动
            _animator.SetLookAtWeight(1, 0.5f);
            //设置权重的代码必须有,不然模型不响应
            _animator.SetLookAtPosition(sphereHead.position);

            //左脚跟随目标物位移   AatarIKGoal枚举 还有rightFoot,leftHand,rightHand
            _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);
            _animator.SetIKPosition(AvatarIKGoal.LeftFoot, sphereLeftFoot.position);

            //左手跟随目标物旋转   AatarIKGoal枚举 还有rightFoot,leftHand,rightHand
            _animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
            _animator.SetIKRotation(AvatarIKGoal.LeftHand, sphereLeftHand.rotation);

        }
        else
        {
            //模型复位,回到模型最初的状态
            _animator.SetLookAtWeight(0);
            _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot,0);
            _animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 0);
        }
    }
}
 
---------------------  
作者:差点忘记打铁了  
来源:优快云  
原文:https://blog.youkuaiyun.com/Fenglele_Fans/article/details/80540798  
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值