获取左右手柄物体:
GameObject handObj = VRTK_DeviceFinder.GetControllerLeftHand(); GameObject handObj = VRTK_DeviceFinder.GetControllerRightHand();
获取左右手柄设备:
Device deciveLeft = SteamVR_Controller.Input((int)VRTK_DeviceFinder.GetControllerIndex(VRTK_DeviceFinder.GetControllerLeftHand()));
Device deciveRight = SteamVR_Controller.Input((int)VRTK_DeviceFinder.GetControllerIndex(VRTK_DeviceFinder.GetControllerRightHand()));
手柄按键:
SteamVR_Controller.ButtonMask.System; SteamVR_Controller.ButtonMask.ApplicationMenu; SteamVR_Controller.ButtonMask.Grip; SteamVR_Controller.ButtonMask.Touchpad; SteamVR_Controller.ButtonMask.Trigger;
手柄上的按键是否按下:
if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("用press按下了 “trigger” “扳机键”");
}
if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("用press按了 “trigger” “扳机键”");
}
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("用press松开了 “trigger” “扳机键”");
}
圆盘各个方向判断:
//触摸触发
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad))
{
Debug.Log("按下了 “Touchpad” “ ”");
//方法返回一个座标 接触圆盘位置
Vector2 cc = device.GetAxis();
Debug.Log(cc);
// 例子:圆盘分成上下左右
float jiaodu = VectorAngle(new Vector2(1, 0), cc);
Debug.Log(jiaodu);
//下
if (jiaodu > 45 && jiaodu < 135)
{
Debug.Log("下");
}
//上
if (jiaodu < -45 && jiaodu > -135)
{
Debug.Log("上");
}
//左
if ((jiaodu < 180 && jiaodu > 135) || (jiaodu < -135 && jiaodu > -180))
{
Debug.Log("左");
}
//右
if ((jiaodu > 0 && jiaodu < 45) || (jiaodu > -45 && jiaodu < 0))
{
Debug.Log("右");
}
}
//按动触发
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
{
Debug.Log("用press按下了 “Touchpad” “ ”");
}
}
//方向圆盘最好配合这个使用 圆盘的.GetAxis()会检测返回一个二位向量,可用角度划分圆盘按键数量
//这个函数输入两个二维向量会返回一个夹角 180 到 -180
float VectorAngle(Vector2 from, Vector2 to)
{
float angle;
Vector3 cross = Vector3.Cross(from, to);
angle = Vector2.Angle(from, to);
return cross.z > 0 ? -angle : angle;
}