unity 人物IK设置 与乒乓函数

论坛 期权论坛 期权     
匿名网站用户   2020-12-21 11:19   6169   0
using UnityEngine;

[RequireComponent(typeof(Animator))] // 加保护 如果没有Animator组件就加上这个脚本再运行
public class IKControl : MonoBehaviour
{
    protected Animator animator;  //给Animator一个对象
    public bool ikActive = false;   //公开设置一个是否跟随移动的选项
    public Transform rightHandObj = null;//公开一个 右手的目标
    public Transform lookObj = null;//公开一个看向的目标
    void Start()
    {
        animator = GetComponent<Animator>();//获得对象的组件
    }
    void OnAnimatorIK()//和Update类似,但不能写在Update中,否则有些方法不能用
    {
        if (animator) //加保护  如果有这个对象就执行
        {
            if (ikActive) //如果勾选,就把获得目标位置运行
            { 
                if (lookObj != null)  //如果看向的对象不为空
                {
                    animator.SetLookAtWeight(1);//0-1区间   权重的写法  (自认为是优先度的体现)
                    animator.SetLookAtPosition(lookObj.position); //看向目标的位置
                }
                if (rightHandObj != null)//如果有右手的目标,确定手的位置跟旋转角度
                {
                    animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);//设定权重  AvatarIKGoal是枚举里面有手脚的位置
                    animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);//旋转权重
                    animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);//手的位置朝向目标位置
                    animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);//手的旋转与目标同步
                }
            } 
            else //如果没有目标
            {
                animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);//设置权重 对模型没影响
                animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 0);//设置权重 对模型没影响
                animator.SetLookAtWeight(0);//设置权重 对模型没影响
            }
        }
    }
}
 transform.Translate(new Vector3 (-0.01f+Mathf.PingPong(Time.time*0.01f,0.02f),0,0));//设置乒乓函数pingpong(速度,范围)
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP