【部分原创】Unity HTC vive手柄的交互,获取手柄事件以及手柄位置等信息

认识下vive的手柄

请看下图


手柄两个一对,分左右,开发的时候也是分左右的。每个上面有一个pad和4个按钮。简单说明下:

  • 开关、系统菜单按钮:只有这个按钮不可以编程(默认),用来打开手柄,其实没用关的功能。在游戏中按下该按钮是调出系统默认的菜单,用来关闭,切换游戏用的。
  • menu按钮:默认用来打开游戏菜单。
  • grip按钮:用的最少的按钮,每个手柄上虽然有两个,但是是相同的。
  • trigger按钮:扳机按钮,用的最多,可以有力度。
  • pad:触摸屏+鼠标的功能,可触摸,可点击。

SDK下载

需要两个插件:

SteamVR Plugin

Vive Input Utility

商城地址如下,都是免费的,下载后导入

https://www.assetstore.unity3d.com/cn/#!/content/32647

https://www.assetstore.unity3d.com/cn/#!/content/64219


程序开发之综述

首先,引用HTC.UnityPlugin.Vive
[csharp]  view plain  copy
  1. using HTC.UnityPlugin.Vive;  


每个按钮包括pad都有GetPress、GetPressDown、GetPressUp三种方法,用HandRole枚举来确定左右手柄,用ControllerButton枚举来确定是哪个按钮。

对于按钮,GetPressDown是按下时触发,GetPressUp是放开时触发,以上两个是个事件,GetPress是按住时一直返回ture,算是一个状态。

对于pad,有两种:

当ControllerButton.Pad时,和按钮相同。

当ControllerButton.PadTouch时,GetPressDown是接触时触发,GetPressUp是离开时触发,GetPress是接触时一直返回的状态。

[csharp]  view plain  copy
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using HTC.UnityPlugin.Vive;  
  5.   
  6. public class viveLearn : MonoBehaviour {  
  7.       
  8.     // Update is called once per frame  
  9.     void Update () {  
  10.         if (ViveInput.GetPress (HandRole.RightHand, ControllerButton.Menu)) {  
  11.             Debug.Log ("menu press");  
  12.         }  
  13.   
  14.         if (ViveInput.GetPressUp (HandRole.RightHand, ControllerButton.Menu)) {  
  15.             Debug.Log ("menu press up");  
  16.         }  
  17.   
  18.         if (ViveInput.GetPressDown (HandRole.RightHand, ControllerButton.Menu)) {  
  19.             Debug.Log ("menu press down");  
  20.         }  
  21.     }  
  22. }  



除了上面的方法,还可以通过回掉的方式实现

[csharp]  view plain  copy
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using HTC.UnityPlugin.Vive;  
  5.   
  6. public class viveLearn : MonoBehaviour {  
  7.   
  8.     private void Awake(){  
  9.         ViveInput.AddPress (HandRole.LeftHand, ControllerButton.Menu, OnMenuPress);  
  10.     }  
  11.   
  12.     private void OnDestory(){  
  13.         ViveInput.RemovePress (HandRole.LeftHand, ControllerButton.Menu, OnMenuPress);  
  14.     }  
  15.   
  16.     private void OnMenuPress(){  
  17.         Debug.Log ("menu press");  
  18.     }  
  19. }  

Trigger按钮开发

Trigger有模拟值,从0到1,没按的时候是0,全部按下是1。可以通过GetTriggerValue方法获得。

[csharp]  view plain  copy
  1. ViveInput.GetTriggerValue (HandRole.LeftHand, false);  


Trigger按钮,对应有三种,Trigger,HairTrigger,FullTrigger。

当TriggerValue在0.1到0.2时,对应HairTrigger,当TriggerValue超过0.5时对应Trigger,当TriggerValue=1时,对应FullTrigger。

可以简单理解为,轻按=HairTriiger,中度按=Trigger,全部按下=FullTrigger。


Pad开发

pad做那么大,当然除了可以按,还可以返回位置信息。用GetPadAxis方法即可。

[csharp]  view plain  copy
  1. ViveInput.GetPadAxis (HandRole.LeftHand, false);  
触碰位置信息如下图



此外,对应pad,又有接触、按下的两组方法。其中,Axis是坐标位置,Delta是最后一帧移动位置,Vector是移动的向量。

[csharp]  view plain  copy
  1. ViveInput.GetPadTouchAxis (HandRole.LeftHand, false);  
  2. ViveInput.GetPadTouchDelta (HandRole.LeftHand, false);  
  3. ViveInput.GetPadTouchVector (HandRole.LeftHand, false);  
  4.   
  5. ViveInput.GetPadPressAxis (HandRole.LeftHand, false);  
  6. ViveInput.GetPadPressDelta (HandRole.LeftHand, false);  
  7. ViveInput.GetPadPressVector (HandRole.LeftHand, false);  

来源 :https://blog.csdn.net/wuyt2008/article/details/54090267

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VRqiegeciganxian : MonoBehaviour {

	public lightControl ll;

	public Camera ca;

	private Quaternion q;

	private Vector3 mousePos;
	private Vector3 preMousePos;
	private Vector3 modelPos;

	private Vector3 localEluer;

	private bool IsSelect = false;
	public float RotateAngle;

	private float angle;

	public Transform target1;

	// Use this for initialization
	void Start () {
		ll.GetComponent<lightControl> ();
		modelPos = ca.WorldToScreenPoint(target1.transform.position);
		angle = localEluer.z;
		target1.transform.localEulerAngles = localEluer;
	}

	// Update is called once per frame
	void Update () {

		if(ViveInput.GetPressDown(HandRole.RightHand,ControllerButton.Trigger)){
			IsSelect = true;
			preMousePos = mousePos = ViveInput.GetPadAxis(HandRole.RightHand,false);
		}
		if (ViveInput.GetPress(HandRole.RightHand,ControllerButton.Trigger) && IsSelect) {
			IsSelect = true;
			mousePos = ViveInput.GetPadAxis(HandRole.RightHand,false);
			RotateAngle = Vector3.Angle (preMousePos - modelPos, mousePos - modelPos);
			print (RotateAngle);

			if (RotateAngle == 0) {
				preMousePos = mousePos;
			}
			else {
				q = Quaternion.FromToRotation (preMousePos - modelPos,mousePos - modelPos);
				float k = q.z > 0 ? 1 : -1;
				localEluer.z += k * RotateAngle;

				//Debug.Log (localEluer.x);

				angle = localEluer.z = Mathf.Clamp (localEluer.z,-36000,36000);

				target1.transform.localEulerAngles = localEluer;
				preMousePos = mousePos;
			}

		}
		if(ViveInput.GetPressUp(HandRole.RightHand,ControllerButton.Trigger)){
			IsSelect = false;
		}
	}
}


猜你喜欢

转载自blog.csdn.net/qq_34272906/article/details/80064550
今日推荐