在unity如何获取VR手柄的按钮
1.首先创建一个C#Script并绑定在任意gameobect上,
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine.Events;
using Valve.VR;
using Valve.VR.InteractionSystem;
namespace Valve.VR.Extras
{
public class Press : MonoBehaviour
{
SteamVR_Behaviour_Pose pose;
public SteamVR_Action_Boolean teleport = SteamVR_Input.GetBooleanAction("Teleport");
private GameObject behaviourR;
// Use this for initialization
void Start()
{
behaviourR = GameObject.Find("RightHand");
pose = behaviourR.GetComponent<SteamVR_Behaviour_Pose>();
}
// Update is called once per frame
void Update()
{
if (teleport.GetStateDown(pose.inputSource))
{
x = true;
}
else if (teleport.GetStateUp(pose.inputSource))
{
x = false;
}
}
}
}
这是控制手柄Teleport键的代码 ↓
public SteamVR_Action_Boolean teleport = SteamVR_Input.GetBooleanAction("Teleport");
这是控制右手手柄的的代码 ↓
void Start()
{
behaviourR = GameObject.Find("RightHand");
pose = behaviourR.GetComponent<SteamVR_Behaviour_Pose>();
}
如果需要再控制左手手柄,可以改成这样,它主要是获取Player中的RightHand或LightHand上的SteamVR_Behaviour_Pose脚本
void Start()
{
behaviourR = GameObject.Find("RightHand");
pose = behaviourR.GetComponent<SteamVR_Behaviour_Pose>();
behaviourL = GameObject.Find("LightHand");
poseL = behaviourL.GetComponent<SteamVR_Behaviour_Pose>();
}
本脚本主要是控制Update中的X来判断按键是否按下
void Update()
{
if (teleport.GetStateDown(pose.inputSource))
{
x = true;
}
else if (teleport.GetStateUp(pose.inputSource))
{
x = false;
}
}