当前位置:   article > 正文

Unity的HTC VIVE SDK研究(手柄按键功能的研究,比较详细)_vrtk获取手柄输入

vrtk获取手柄输入

转载自:https://www.cnblogs.com/czaoth/p/5610883.html

 

  1. using UnityEngine;
  2. using System.Collections;
  3. //检测手柄功能的脚本 这个脚本挂到手柄上(controler(right)和controler(left))上
  4. public class ButtonTouchAction : MonoBehaviour {
  5. //手柄
  6. SteamVR_TrackedObject trackdeObjec;
  7. void Awake() {
  8. //获取手柄上的这个组件
  9. trackdeObjec = GetComponent<SteamVR_TrackedObject>();
  10. }
  11. // Use this for initialization
  12. void Start () {
  13. }
  14. void FixedUpdate()
  15. { //获取手柄输入
  16. var device = SteamVR_Controller.Input((int)trackdeObjec.index);
  17. //以下是api中复制出来的按键列表
  18. /* public class ButtonMask
  19. {
  20. public const ulong System = (1ul << (int)EVRButtonId.k_EButton_System); // reserved
  21. public const ulong ApplicationMenu = (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu);
  22. public const ulong Grip = (1ul << (int)EVRButtonId.k_EButton_Grip);
  23. public const ulong Axis0 = (1ul << (int)EVRButtonId.k_EButton_Axis0);
  24. public const ulong Axis1 = (1ul << (int)EVRButtonId.k_EButton_Axis1);
  25. public const ulong Axis2 = (1ul << (int)EVRButtonId.k_EButton_Axis2);
  26. public const ulong Axis3 = (1ul << (int)EVRButtonId.k_EButton_Axis3);
  27. public const ulong Axis4 = (1ul << (int)EVRButtonId.k_EButton_Axis4);
  28. public const ulong Touchpad = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Touchpad);
  29. public const ulong Trigger = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Trigger);
  30. }
  31. */
  32. //同样是三种按键方式,以后不做赘述
  33. if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)) {
  34. Debug.Log("按了 “trigger” “扳机键”");
  35. //右手震动
  36. //拉弓类似操作应该就是按住trigger(扳机)gettouch时持续调用震动方法模拟弓弦绷紧的感觉。
  37. var deviceIndex2 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
  38. SteamVR_Controller.Input(deviceIndex2).TriggerHapticPulse(500);
  39. }
  40. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
  41. { Debug.Log("按下了 “trigger” “扳机键”");
  42. }
  43. if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)) {
  44. Debug.Log("松开了 “trigger” “扳机键”");
  45. //左手震动
  46. var deviceIndex = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost);
  47. SteamVR_Controller.Input(deviceIndex).TriggerHapticPulse(3000);
  48. //右手震动
  49. var deviceIndex1 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
  50. SteamVR_Controller.Input(deviceIndex1).TriggerHapticPulse(3000);
  51. }
  52. //这三种也能检测到 后面不做赘述
  53. if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) {
  54. Debug.Log("用press按下了 “trigger” “扳机键”");
  55. }
  56. if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
  57. {
  58. Debug.Log("用press按了 “trigger” “扳机键”");
  59. }
  60. if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
  61. {
  62. Debug.Log("用press松开了 “trigger” “扳机键”");
  63. }
  64. //system键 圆盘下面那个键
  65. // reserved 为Steam系统保留,用来调出Steam系统菜单 因此貌似自己加的功能没啥用
  66. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.System))
  67. {
  68. Debug.Log("按下了 “system” “系统按钮/Steam”");
  69. }
  70. if (device.GetPressDown(SteamVR_Controller.ButtonMask.System))
  71. {
  72. Debug.Log("用press按下了 “System” “系统按钮/Steam”");
  73. }
  74. //ApplicationMenu键 带菜单标志的那个按键(在方向圆盘上面)
  75. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
  76. {
  77. Debug.Log("按下了 “ApplicationMenu” “菜单键”");
  78. }
  79. if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
  80. {
  81. Debug.Log("用press按下了 “ApplicationMenu” “菜单键”");
  82. }
  83. //Grip键 两侧的键 (vive雇佣兵游戏中的换弹键),每个手柄左右各一功能相同,同一手柄两个键是一个键。
  84. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Grip))
  85. {
  86. Debug.Log("按下了 “Grip” “ ”");
  87. }
  88. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
  89. {
  90. Debug.Log("用press按下了 “Grip” “ ”");
  91. }
  92. //Axis0键 与圆盘有交互 与圆盘有关
  93. //触摸触发
  94. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))
  95. {
  96. Debug.Log("按下了 “Axis0” “方向 ”");
  97. }
  98. //按动触发
  99. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))
  100. {
  101. Debug.Log("用press按下了 “Axis0” “方向 ”");
  102. }
  103. //Axis1键 目前未发现按键位置
  104. //触摸触发
  105. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1))
  106. {
  107. Debug.Log("按下了 “Axis1” “ ”");
  108. }
  109. //按动触发
  110. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1))
  111. {
  112. Debug.Log("用press按下了 “Axis1” “ ”");
  113. }
  114. //Axis2键 目前未发现按键位置
  115. //触摸触发
  116. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2))
  117. {
  118. Debug.Log("按下了 “Axis2” “ ”");
  119. }
  120. //按动触发
  121. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2))
  122. {
  123. Debug.Log("用press按下了 “Axis2” “ ”");
  124. }
  125. //Axis3键 未目前未发现按键位置
  126. //触摸触发
  127. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3))
  128. {
  129. Debug.Log("按下了 “Axis3” “ ”");
  130. }
  131. //按动触发
  132. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3))
  133. {
  134. Debug.Log("用press按下了 “Axis3” “ ”");
  135. }
  136. //Axis4键 目前未发现按键位置
  137. //触摸触发
  138. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4))
  139. {
  140. Debug.Log("按下了 “Axis4” “ ”");
  141. }
  142. //按动触发
  143. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4))
  144. {
  145. Debug.Log("用press按下了 “Axis4” “ ”");
  146. }
  147. <pre name="code" class="csharp"> //方向圆盘:
  148. //这里开始区分了press检测与touch检测的不同之处,圆盘可以触摸,顾名思义,touch检测的是触摸,press检测的是按动<pre name="code" class="csharp"> //Axis0键 与圆盘有交互 与圆盘有关
  149. //触摸触发
  150. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))
  151. {
  152. Debug.Log("按下了 “Axis0” “方向 ”");
  153. }
  154. //按动触发
  155. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))
  156. {
  157. Debug.Log("用press按下了 “Axis0” “方向 ”");
  158. }
  159. //Axis1键 目前未发现按键位置
  160. //触摸触发
  161. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1))
  162. {
  163. Debug.Log("按下了 “Axis1” “ ”");
  164. }
  165. //按动触发
  166. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1))
  167. {
  168. Debug.Log("用press按下了 “Axis1” “ ”");
  169. }
  170. //Axis2键 目前未发现按键位置
  171. //触摸触发
  172. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2))
  173. {
  174. Debug.Log("按下了 “Axis2” “ ”");
  175. }
  176. //按动触发
  177. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2))
  178. {
  179. Debug.Log("用press按下了 “Axis2” “ ”");
  180. }
  181. //Axis3键 未目前未发现按键位置
  182. //触摸触发
  183. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3))
  184. {
  185. Debug.Log("按下了 “Axis3” “ ”");
  186. }
  187. //按动触发
  188. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3))
  189. {
  190. Debug.Log("用press按下了 “Axis3” “ ”");
  191. }
  192. //Axis4键 目前未发现按键位置
  193. //触摸触发
  194. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4))
  195. {
  196. Debug.Log("按下了 “Axis4” “ ”");
  197. }
  198. //按动触发
  199. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4))
  200. {
  201. Debug.Log("用press按下了 “Axis4” “ ”");
  202. }
  203. //ATouchpad键 圆盘交互
  204. //触摸触发
  205. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad))
  206. {
  207. Debug.Log("按下了 “Touchpad” “ ”");
  208. //方法返回一个坐标 接触圆盘位置
  209. Vector2 cc = device.GetAxis();
  210. Debug.Log(cc);
  211. // 例子:圆盘分成上下左右
  212. float jiaodu = VectorAngle(new Vector2(1, 0), cc);
  213. Debug.Log(jiaodu);
  214. //下
  215. if (jiaodu > 45 && jiaodu < 135)
  216. {
  217. Debug.Log("下");
  218. }
  219. //上
  220. if (jiaodu < -45 && jiaodu > -135)
  221. {
  222. Debug.Log("上");
  223. }
  224. //左
  225. if ((jiaodu < 180 && jiaodu > 135) || (jiaodu < -135 && jiaodu > -180))
  226. {
  227. Debug.Log("左");
  228. }
  229. //右
  230. if ((jiaodu > 0 && jiaodu < 45) || (jiaodu > -45 && jiaodu < 0))
  231. {
  232. Debug.Log("右");
  233. }
  234. }
  235. //按动触发
  236. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
  237. {
  238. Debug.Log("用press按下了 “Touchpad” “ ”");
  239. }
  240. }
  241. // Update is called once per frame
  242. void Update () {
  243. }
  244. //方向圆盘最好配合这个使用 圆盘的.GetAxis()会检测返回一个二位向量,可用角度划分圆盘按键数量
  245. //这个函数输入两个二维向量会返回一个夹角 180 到 -180
  246. float VectorAngle(Vector2 from, Vector2 to)
  247. {
  248. float angle;
  249. Vector3 cross = Vector3.Cross(from, to);
  250. angle = Vector2.Angle(from, to);
  251. return cross.z > 0 ? -angle : angle;
  252. }
  253. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/99956?site
推荐阅读
相关标签
  

闽ICP备14008679号