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

    }
}
相关推荐
玖玥拾8 小时前
Unity Shader 基础与图形学(一)
unity·图形渲染·图形学·shader
Neil201311 小时前
ajax跨域请求
c#
唐青枫11 小时前
别把 Razor 当成几段 HTML:C#.NET Razor Pages、MVC 视图与实战详解
c#·.net
WiChP15 小时前
【V0.1B16】从零开始的2D游戏引擎开发之路
开发语言·算法·游戏引擎
百里香酚兰16 小时前
【Unity学习笔记】如何把粉色Prefab还原
笔记·学习·unity
专注仿真17 小时前
CMO模型文档-活动单元基类
c#·模型·cmo·活动单元基类
iNeuOS工业互联网18 小时前
大模型(DeepSeek)辅助 3D 实时建模 + 拖拽配置:业务可视化应用快速构建实践(标注、巡检与视角控制等)
人工智能·物联网·3d·语言模型·工业互联网
xcLeigh18 小时前
Unity基础:使用Transform控制物体旋转——Rotate与LookAt详解
unity·教程·transform·rotate·lookat
Zldaisy3d18 小时前
点亮高端制造轻量化新图景:铝基材料增材制造进展与挑战
3d·制造
我是苏苏20 小时前
Agent 开发实战 :C#实现语义检索!向量数据库Qdrant的下载安装及调试步骤
c#·ai编程