관리 메뉴

caLAB

[Htc Vive] VR 컨트롤러 input 본문

Unity/유니티VR

[Htc Vive] VR 컨트롤러 input

도이(doi) 2020. 6. 10. 11:09
728x90

1인칭 시점 움직임

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class firstPerspectMove : MonoBehaviour
{
    public SteamVR_Input_Sources handType;
    public SteamVR_Action_Boolean teleportAction;

    public float speed = 0.05f;
    public Transform cam;

    private float v;

    void Update()
    {
        RotCtrl();
        MoveCtrl();
    }
    // VR이동 
    void MoveCtrl()
    {
        if (teleportAction.GetState(handType))
        {
            v = 1;
        }
        else if (teleportAction.GetStateUp(handType))
        {
            v = 0;
        }

        if (!(v == 0))
        {
            transform.Translate(RotCtrl() * speed * Time.deltaTime * 0.1f);
        }
    }

    // 회전
    public Vector3 RotCtrl()
    {
        Vector3 dir = cam.transform.localRotation * Vector3.forward;
        transform.localRotation = cam.transform.localRotation;
        transform.localRotation = new Quaternion(0, transform.localRotation.y, 0, transform.localRotation.w);
        return dir;
    }

}

 

3인칭 시점 움직임

Input값으로 단일 버튼 값만 받아오기 때문에 직진 이동만 가능함. 

제어를 위한 것으로 다음에 추가적으로 4방향 이동 연구. 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class cloneBehaviour : MonoBehaviour
{
    public SteamVR_Input_Sources handType;
    public SteamVR_Action_Boolean teleportAction;
    public SteamVR_Action_Vector2 touchPad;

    public Animator anim;

    public float speed = 0.05f;

    private float v;

    void Update()
    {
        if (teleportAction.GetState(handType))
        {
            v = 1;
        }
        else if (teleportAction.GetStateUp(handType))
        {
            v = 0;
        }

        if (!( v == 0))
        {
            transform.Translate(Vector3.forward * speed * Time.deltaTime * 0.1f);
            anim.SetFloat("Speed", .15f);
        }
        else
        {
            anim.SetFloat("Speed", 0);
        }
    }
}

 

 

728x90
반응형
Comments