VR 콘텐츠 기초

[Meta Quest, VR, Oculus] 손가락에서 Ray, Line Renderer 쏘기, 핸드 트리거 누르면 Line 쏘기(Hand Trigger), 목표거리까지 Line Renderer 생성하기

다모아 2023. 10. 26. 15:58

1.  b_l_index_null 3번째꺼가 검지 손가락 끝에 위치함

2.  Project Settings > Player > Rendering > Graphics APIs > + 버튼으로 OpenGLES3 추가

 

3. XR Plug-in Management > Oculus > Stereo Rendering Mode > Multi Pass 로 변경

 

4.  쏘고싶은 손 XXXHand > HandInteractorsXXX의 자식으로 Empty GameObject 생성 후 UIHandRayInteractor 이름변경

 

5. UIHandRayInteractor의 자식으로 Line 생성

라인의 Width 마지막에 우클릭해서 Add Key를 해주고 저렇게 내려주면 Line의 마지막이 얇아짐

 

6. UI Hand Ray Interactor Script

 

결과


Line Renderer 그리기 전 Ray 쏴보기

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

public class UIHandRayInteractor : MonoBehaviour
{
    [SerializeField] private Transform b_l_index_null;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        Debug.DrawRay(b_l_index_null.position, -b_l_index_null.right * 5f,Color.red);
    }
}

Map Controller

https://developer.oculus.com/documentation/unity/unity-ovrinput/

 

Map Controllers | Oculus Developers

 

developer.oculus.com

컨트롤러 키 사용방법 나와있음


Trigger 키 누르면 Line Renderer 나오게 설정하기

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

public class UIHandRayInteractor : MonoBehaviour
{
    [SerializeField] private Transform b_l_index_null;
    [SerializeField] private LineRenderer lineRenderer;

    // Start is called before the first frame update
    void Start()
    {
    }

    private void FixedUpdate()
    {
        //Debug.DrawRay(b_l_index_null.position, -b_l_index_null.right * 5f,Color.red);
        this.lineRenderer.SetPosition(0, b_l_index_null.position);

        float handTrigger = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, OVRInput.Controller.Touch);

        if(handTrigger > 0)
        {
            this.lineRenderer.enabled = true;
            this.lineRenderer.SetPosition(1, b_l_index_null.position + -b_l_index_null.right * 5f);
        }
        else
        {
            this.lineRenderer.enabled = false;
        }
    }
}


Cube 만들고 Trigger 누르면 Line Renderer가 Cube까지만 거리가 보이기

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

public class UIHandRayInteractor : MonoBehaviour
{
    [SerializeField] private Transform b_l_index_null;
    [SerializeField] private LineRenderer lineRenderer;

    // Start is called before the first frame update
    void Start()
    {
    }

    private void FixedUpdate()
    {
        this.lineRenderer.enabled = true;

        //Debug.DrawRay(b_l_index_null.position, -b_l_index_null.right * 5f,Color.red);
        this.lineRenderer.SetPosition(0, b_l_index_null.position);

        float handTrigger = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, OVRInput.Controller.Touch);

        if(handTrigger > 0)
        {
            //핸드트리거를 눌렀다면 레이캐스트
            Ray ray = new Ray(b_l_index_null.position, -b_l_index_null.right);
            RaycastHit hit;
            if (Physics.Raycast(ray.origin, ray.direction, out hit, 5f))
            {
                //hit된 gameObject까지의 거리 가져오기
                float dis = Vector3.Distance(b_l_index_null.position, hit.point);
                //hit된 거리까지만 Line 그리기
                this.lineRenderer.SetPosition(1, b_l_index_null.position + -b_l_index_null.right * dis);
            }
            else
            {
                //아니라면 Line 길게 그리기
                this.lineRenderer.SetPosition(1, b_l_index_null.position + -b_l_index_null.right * 5f);
            }
            
        }
        else
        {
            this.lineRenderer.enabled = false;
        }
    }
}

결과