有什么方法可以缓慢平滑地更改Animator IK SetLookAtWeight?

问题描述

using UnityEngine;
using System;
using System.Collections;

[RequireComponent(typeof(Animator))]

public class IKControl : MonoBehavIoUr
{

    protected Animator animator;

    public bool ikActive = false;
    public Transform headobj = null;
    public Transform lookObj = null;

    private bool changeWeight = false;
    private float t = 0;
    private float value = 0;

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

    //a callback for calculating IK
    void OnAnimatorIK()
    {
        if (animator)
        {

            //if the IK is active,set the position and rotation directly to the goal. 
            if (ikActive)
            {
                changeWeight = false;

                // Set the look target position,if one has been assigned
                if (lookObj != null)
                {
                    animator.SetLookAtWeight(1,1,1);
                    animator.SetLookAtPosition(lookObj.position);
                }
            }

            //if the IK is not active,set the position and rotation of the hand and head back to the original position
            else
            {
                changeWeight = true;
            }
        }
    }

    private void Update()
    {
        if(changeWeight == true)
        {
            t += Time.deltaTime / 5f;
            value = Mathf.Lerp(1f,0f,t);

            animator.SetLookAtWeight(value);
        }
    }
}

我正在尝试使用一个标志,并在Update中缓慢地更改该值,因此SetLookAtWeight在这种情况下仅需5秒钟即可进行测试。

但是SetLookAtWeight值就像从1跳到0,并且没有从1慢慢变到0。 当我运行游戏并将bool标志ikActive设置为false时,玩家的头会直接从注视模式立即更改为空闲模式。

我希望它缓慢地变为空闲模式。

解决方法

您永远不会重置t的值。在玩了5秒钟后,它将大于1,因此Lerp将始终返回最大值。

尝试将此行添加到if (ikActive)块的顶部 t = 0f;