当前位置:   article > 正文

unity开发 HTC vive手柄控制 手柄上各个按钮的点击事件_unity htc cosmos手柄事件

unity htc cosmos手柄事件
HTC vive手柄各个按钮的响应事件,已实现的功能是按手柄上的原盘上下左右键移动,是平移,不是射线瞬移。这种方式在眼镜里太晕了。
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Move : MonoBehaviour {
  4. /// <summary>
  5. /// 手柄位置
  6. /// </summary>
  7. SteamVR_TrackedObject tracked;
  8. /// <summary>
  9. /// 玩家
  10. /// </summary>
  11. public Transform player;
  12. /// <summary>
  13. /// 方向
  14. /// </summary>
  15. public Transform dic;
  16. /// <summary>
  17. /// 速度
  18. /// </summary>
  19. public float speed;
  20. void Awake()
  21. {
  22. //获取手柄控制
  23. tracked = GetComponent<SteamVR_TrackedObject>();
  24. }
  25. // Use this for initialization
  26. void Start()
  27. {
  28. }
  29. // Update is called once per frame
  30. void FixedUpdate()
  31. {
  32. var deviceright = SteamVR_Controller.Input((int)tracked.index);
  33. //按下圆盘键
  34. if (deviceright.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
  35. {
  36. Vector2 cc = deviceright.GetAxis();
  37. float angle = VectorAngle(new Vector2(1, 0), cc);
  38. //下
  39. if (angle > 45 && angle < 135)
  40. {
  41. player.Translate(-dic.forward * Time.deltaTime * speed);
  42. }
  43. //上
  44. else if (angle < -45 && angle > -135)
  45. {
  46. //Debug.Log("上");
  47. player.Translate(dic.forward * Time.deltaTime * speed);
  48. }
  49. //左
  50. else if ((angle < 180 && angle > 135) || (angle < -135 && angle > -180))
  51. {
  52. //Debug.Log("左");
  53. player.Translate(-dic.right * Time.deltaTime * speed);
  54. }
  55. //右
  56. else if ((angle > 0 && angle < 45) || (angle > -45 && angle < 0))
  57. {
  58. //Debug.Log("右");
  59. player.Translate(dic.right * Time.deltaTime * speed);
  60. }
  61. }
  62. else if (deviceright.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
  63. {
  64. Debug.Log("按下扳机键");
  65. }
  66. else if (deviceright.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
  67. {
  68. Debug.Log("按下手柄侧键");
  69. }
  70. else if (deviceright.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
  71. {
  72. Debug.Log("按下手柄菜单键");
  73. }
  74. else if (deviceright.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
  75. {
  76. Debug.Log("按下手柄菜单键");
  77. }
  78. }
  79. /// <summary>
  80. /// 根据在圆盘才按下的位置,返回一个角度值
  81. /// </summary>
  82. /// <param name="from"></param>
  83. /// <param name="to"></param>
  84. /// <returns></returns>
  85. float VectorAngle(Vector2 from, Vector2 to)
  86. {
  87. float angle;
  88. Vector3 cross = Vector3.Cross(from, to);
  89. angle = Vector2.Angle(from, to);
  90. return cross.z > 0 ? -angle : angle;
  91. }
  92. }

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

闽ICP备14008679号