Unity VR Leg IK

이번 시간엔 Leg Ik 설정하는 방법에 대해 알아보겠다

먼저 모델의 Animation Type을 Humanoid로 바꿔준다

그다음 믹사모같은데에서 Walk관련 애니메이션을 하나 다운받아준다

그 애니메이션 또한 휴머노이드로 바꿔주고

캐릭터애니메이션을 넣어준다

이렇게

 

그다음으로

새로운 아바타 마스크를 만들어주고 상체부분을 꺼준다

Base Layer에 방금 만든 아바타 마스크를 집어넣어준다

IKPass도 체크해준다

 

다음으로 VRLegIK 스크립트를 하나 생성해준다

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VRFootIK : MonoBehaviour
{
    private Animator _animator;

    public Vector3 footOffest;
    
    [Range(0,1)]
    public float rigthFootPosWeight = 1f;
    [Range(0,1)]
    public float rigthRotPosWeight = 1f;
    [Range(0,1)]
    public float leftFootPosWeight = 1f;
    [Range(0,1)]
    public float leftRotPosWeight = 1f;
    private void Awake()
    {
        _animator = GetComponent<Animator>();
        
    }

    private void OnAnimatorIK(int layerIndex)
    {
        Vector3 rightFootPos = _animator.GetIKPosition(AvatarIKGoal.RightFoot);
        RaycastHit hit;

        bool hasHit = Physics.Raycast(rightFootPos + Vector3.up, Vector3.down, out hit);
        if (hasHit)
        {
            _animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, rigthFootPosWeight);
            _animator.SetIKPosition(AvatarIKGoal.RightFoot, hit.point + footOffest);
            
            Quaternion rightRootRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(transform.forward, hit.normal), hit.normal);
            _animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, rigthRotPosWeight);
            _animator.SetIKRotation(AvatarIKGoal.RightFoot , rightRootRotation);
        }
        else
        {
            _animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 0);
        }
        
        Vector3 leftFootPos = _animator.GetIKPosition(AvatarIKGoal.LeftFoot);

        hasHit = Physics.Raycast(leftFootPos + Vector3.up, Vector3.down, out hit);
        if (hasHit)
        {
            _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, leftFootPosWeight);
            _animator.SetIKPosition(AvatarIKGoal.LeftFoot, hit.point + footOffest);
        }
        else
        {
            _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 0);
            
            Quaternion leftRootRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(transform.forward, hit.normal), hit.normal);
            _animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, leftRotPosWeight);
            _animator.SetIKRotation(AvatarIKGoal.LeftFoot , leftRootRotation);
        }
    }
}

 

footOffest같은 경우 적절하게 입맛에 맞게 설정하자

나는 0.12로 했다