獲取左右手柄物體:
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;
}