如何使用IK动画师查看两个或更多对象?

问题描述

我在另一个位置分别有两个立方体。 问题在于,角色面对多维数据集时只能看着其中一个多维数据集。

但是当我旋转角色使其面向第二个立方体时,他却再也没有看到它。

using UnityEngine;
using System;
using System.Collections;
using UnityEngine.socialPlatforms;
using UnityEditor.Animations;
 
[RequireComponent(typeof(Animator))]
 
public class IKControl : MonoBehavIoUr
{
    public Transform[] lookObj = null;
    public float finalLookWeight;
    public float weightdamping = 1.5f;
    public float playerSpeedRot;
    public bool rotatePlayer = false;
 
    protected Animator animator;
 
    void Start()
    {
        animator = GetComponent<Animator>();
    }
 
    //a callback for calculating IK
    void OnAnimatorIK()
    {
        if (lookObj != null)
        {
            for (int i = 0; i < lookObj.Length; i++)
            {
                Vector3 flattenedLookAtVector = Vector3.ProjectOnPlane(lookObj[i].position - transform.position,transform.up);
                float dotProduct = Vector3.Dot(transform.forward,flattenedLookAtVector);
                float lookWeight = Mathf.Clamp(dotProduct,0f,1f);
                finalLookWeight = Mathf.Lerp(finalLookWeight,lookWeight,Time.deltaTime * weightdamping);
                float bodyWeight = finalLookWeight * .5f;
 
                animator.SetLookAtWeight(finalLookWeight,bodyWeight);
                animator.SetLookAtPosition(lookObj[i].position);
 
                if (rotatePlayer == true)
                {
                    var lookPos = lookObj[i].position - transform.position;
                    lookPos.y = 0;
                    var rotation = Quaternion.LookRotation(lookPos);
                    transform.rotation = Quaternion.Slerp(transform.rotation,rotation,Time.deltaTime * playerSpeedRot);
                }
            }
        }
    }
}

我正在使用for循环检测所有多维数据集,但他只查看其中一个多维数据集。

Two cubes

当我移动并旋转面对左侧立方体的角色时,它将平滑地看着立方体旋转角色头部。但是当我移动和旋转面向右侧立方体的角色时,他不会旋转只看着左侧立方体的头部。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)