unity 人物IK設置 與乒乓函數

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(速度,範圍)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章