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);

    }
}
相关推荐
雨落倾城夏未凉21 小时前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫2 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫3 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6253 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902113 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠4 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫6 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech6 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf8 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6258 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#