Unity-动画IK控制

什么是 IK?在骨骼动画中,构建骨骼的方法被称为正向动力学

它的表现形式是,子骨骼(关节)的位置根据父骨骼(关节)的旋转而改变

用我们人体举例子,当我们抬起手臂时,是肩部关节带动的整个手臂的运动,用父子骨骼理解的话就是父带动了子

而 IK 全称是 Inverse Kinematics,翻译过来的意思就是反向动力学的意思

它和正向动力学恰恰相反

它的表现形式是,子骨骼(关节)末端的位置改变会带动自己以及自己的父骨骼(关节)旋转

用我们人体举例子,当我们拿起一个杯子的时候是用手掌去拿,以杯子为参照物,我们移动杯子的位置,手臂会随着杯子一起移动用父子骨骼理解的话就是子带动了父

如何进行 IK 控制

在状态机的层级设置中 开启 IK 通道

继承 MonoBehavior 的类中Unity 定义了一个 IK 回调函数:OnAnimatorIK我们可以在该函数中调用 Unity 提供的 IK 相关 API 来控制 IK

Animator 中的 IK 相关 API

SetLookAtWeight 设置头部 IK 权重

SetLookAtPosition 设置头部 IK 看向位置

SetIKPositionWeight 设置 IK 位置权重

SetIKRotationWeight 设置 IK 旋转权重

SetIKPosition 设置 IK 对应的位置

SetIKRotation 设置 IK 对应的角度

AvatarIKGoal 枚举 四肢末端 IK 枚举

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lesson16 : MonoBehaviour
{
    public Transform pos;
    public Transform rightHandpos;
    public Transform leftHandpos;
    public Transform rightFootpos;
    public Transform leftFootpos;
    private Animator animator;
    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    /// <summary>
    /// 处理IK相关逻辑
    /// </summary>
    /// <param name="layerIndex"></param>
    private void OnAnimatorIK(int layerIndex)
    {
        //头部IK相关
        //weight:LookAt全局权重0-1
        //bodyWeight:LookAt时身体的权重0-1
        //headWeight:LookAt时头部的权重0-1
        //eyesWeight:LookAt眼睛的权重0-1
        //clampWeight:0表示角色运动时不受限制,1表示角色完全固定无法执行LookAt,0.5表示只能够移动范围的一半
        animator.SetLookAtWeight(1,0.5f,0.5f);
        animator.SetLookAtPosition(pos.position);


        //四肢IK相关
        animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1f);
        animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1f);
        animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandpos.position);
        animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandpos.rotation);

        animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1f);
        animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1f);
        animator.SetIKPosition(AvatarIKGoal.RightFoot, rightFootpos.position);
        animator.SetIKRotation(AvatarIKGoal.RightFoot, rightFootpos.rotation);

        animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1f);
        animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1f);
        animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandpos.position);
        animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandpos.rotation);

        animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1f);
        animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1f);
        animator.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootpos.position);
        animator.SetIKRotation(AvatarIKGoal.LeftFoot, leftFootpos.rotation);
    }
    /// <summary>
    /// 如果动画本身有移动,但还想自己写移动逻辑可以在这个函数里操作
    /// </summary>
    private void OnAnimatorMove()
    {
        
    }
}

关于 OnAnimatorIK 和 OnAnimatorMove 两个函数的理解

我们可以简单理解这两个函数是两个和动画相关的特殊生命周期函数

他们在 Update 之后 LateUpdate 之前调用

他们会在每帧的状态机和动画处理完后调用

OnAnimatorIK 在 OnAnimatorMove 之前调用

OnAnimatorIK 中主要处理 IK 运动相关逻辑

OnAnimatorMove 主要处理 动画移动以修改根运动的回调逻辑

他们存在的目的只是多了一个调用时机,当每帧的动画和状态机逻辑处理完后再调用

鼠标控制角色看向方向

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lesson15 : MonoBehaviour
{
    public Transform transformPos;
    private Animator animator;
    private float x;
    private float y;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        animator.SetFloat("x",Input.GetAxis("Horizontal"));
        animator.SetFloat("y", Input.GetAxis("Vertical"));

        if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("New Trigger");
        }
        x += Input.GetAxis("Mouse X");
        x = Mathf.Clamp(x, -30, 30);
        y += Input.GetAxis("Mouse Y");
        y = Mathf.Clamp(y, -30, 30);
    }

    private void OnAnimatorIK(int layerIndex)
    {
        animator.SetLookAtWeight(1, 0.5f, 0.5f);
        Vector3 pos = Quaternion.AngleAxis(x, Vector3.up) * (transformPos.position + transformPos.forward * 10);
        pos = Quaternion.AngleAxis(-y, Vector3.right) * pos;
        animator.SetLookAtPosition(pos);

    }
}
相关推荐
农场主er9 小时前
Metal - 5.深入剖析 3D 变换
3d·opengl·transform·matrix·metal
一朵小红花HH10 小时前
SimpleBEV:改进的激光雷达-摄像头融合架构用于三维目标检测
论文阅读·人工智能·深度学习·目标检测·机器学习·计算机视觉·3d
时光追逐者11 小时前
一个基于 .NET 开源、简易、轻量级的进销存管理系统
开源·c#·.net·.net core·经销存管理系统
CG_MAGIC12 小时前
Blender 创作手记:皮椅的针织布皮革质感塑造之旅
3d·blender·建模教程·pointiness 节点
Ziegler Han12 小时前
Unity入门:给Image添加图片
unity·游戏引擎
William_cl12 小时前
【连载7】 C# MVC 跨框架异常处理对比:.NET Framework 与 .NET Core 实现差异
c#·mvc·.net
ajassi200012 小时前
开源 C# 快速开发(五)自定义控件--仪表盘
开发语言·开源·c#
Dream achiever13 小时前
3.WPF - 依赖属性
c#·wpf
mudtools14 小时前
.NET操作Excel:高级格式设置
c#·.net·excel·wps