unity 人物IK设置 与乒乓函数

15 篇文章 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(速度,范围)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值