본문 바로가기
Unity

[Unity] XR Controller - Haptic

by imagineer_jinny 2022. 1. 10.

VR 게임 프로젝트 중 Haptic Interaction 기능 추가

 

문제:

xr controller (device based) 추가 하고

glaze 스크립트에 

using UnityEngine.XR.Interaction.Toolkit;

하고 링크에 있는 코드들 넣어주면 동작함

근데 오른쪽만 동작함

 

[유니티 VR] 컨트롤러 인터렉션 (tistory.com)

 

[유니티 VR] 컨트롤러 인터렉션

컨트롤러 Haptic 기능 넣기 XR controller 스크립트를 보게 되면 아래와 같은 함수가 보인다. public override bool SendHapticImpulse(float amplitude, float duration) { if (inputDevice.TryGetHapticCapabili..

computer-art.tistory.com

 

 

문제 해결:

detect되는 컨트롤러를 명시해서 haptic 기능을 실행

+ 인터렉트 한 컨트롤러만 haptic이 반응하도록 설정

 

Glaze.cs

public XRBaseControllerInteractor leftController, rightController;
public HandDetector handDetector;
public grabbingHand hand;

 void ActivateHaptic()
        {
            if (hand == grabbingHand.left)
            {
                leftController.SendHapticImpulse(0.3f, 0.5f);
            }
            else
            {
                rightController.SendHapticImpulse(0.3f, 0.5f);
            }
        }
        
 
 public void GrabGlaze()
        {
            isGrab = true;
            StartCoroutine(IUpdate());

            // 잡고 있는 손이 어느 손인지 확인하는 스크립트
            hand = handDetector.DetectHand();
        }
 public void DeActiveGrabGlaze()
        {
            isGrab = false;
            hand = grabbingHand.none;
        }

 

HandDetector.cs

using UnityEngine.XR.Interaction.Toolkit;
using DonutEnum;

public class HandDetector : XRGrabInteractable
{
    public grabbingHand DetectHand()
    {
        if(selectingInteractor.name== "RightHand Controller")
        {
            // 잡고 있는 손이 오른손일 경우
            return grabbingHand.right;
        } else
        {
            // 잡고 있는 손이 오른손이 아닐 경우 == 왼손일 경우
            return grabbingHand.left;
        }

    }
   
}

 

댓글