手动为角色绑定IK动画

作者:追风剑情 发布于:2019-5-6 20:43 分类:Unity3d

[官方文档] 反向动力学

可在 Mecanim 中用于已正确配置的任何人形Avatar骨骼。

一、导入角色模型,并进行设置

3333.png

二、创建两个空对象IK_LeftHand、IK_RightHand用来对左右手部进行定位。

4444.png3333.png

三、创建AnimatorController并创建一个空动画

3333.png

启用IK Pass

3333.png

3333.png

四、在场景中的模型对象上挂上ArmIK脚本

ArmIK.cs


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

public class ArmIK : MonoBehaviour
{
    public float leftHandPositionWeight;
    public float leftHandRotationWeight;

    public float rightHandPositionWeight;
    public float rightHandRotationWeight;

    public Transform leftHandObj;
    public Transform rightHandObj;
    private Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    private void OnAnimatorIK(int layerIndex)
    {
        Debug.Log("***** OnAnimatorIK");

        animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandPositionWeight);
        animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandRotationWeight);
        animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.position);
        animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.rotation);

        animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandPositionWeight);
        animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandRotationWeight);
        animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
        animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
    }
}


运行并拖动IK_LeftHand和IK_RightHand两个空对象观察手臂摆动效果

2.gif

1111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号