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

    }
}
相关推荐
承渊政道13 分钟前
动态内存管理
c语言·c++·经验分享·c#·visual studio
future_studio16 分钟前
聊聊 Unity(小白专享、C# 小程序 之 播放器)
unity·小程序·c#
helloworddm1 小时前
Orleans Stream SubscriptionId 生成机制详解
java·系统架构·c#
小Tomkk1 小时前
Rokid 开发空间小程序 实战
3d·小程序·rokid·jsar
向宇it1 小时前
【unity实战】MapMagic 2实战例子
游戏·3d·unity·c#·游戏引擎
"菠萝"2 小时前
C#知识学习-017(修饰符_6)
学习·c#
VB.Net2 小时前
C#循序渐进
开发语言·c#
SlowFeather3 小时前
Unity TMP可控角度多色渐变文字
unity·游戏引擎
feifeigo1233 小时前
C# WinForms实现模拟叫号系统
c#
helloworddm4 小时前
Orleans 流系统握手机制时序图
后端·c#