谈到Unity VR开发,即使是初学者也会听过SteamVR或者HTC VIVE,这款设备是目前在国内能够方便买到且在使用上不错的一款设备。其实SteamVR是集成在Steam里的一个VR插件,而HTC是硬件设备,运行时用到SteamVR这个插件(也是HTC运行时必须的环境),对于SteamVR在前面一篇有介绍,它是基于OpenVR开发的一套开源的插件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ImportedObject : MonoBehaviour {
private SteamVR_TrackedObject trackedOjbect;
private SteamVR_Controller.Device device;
private GameObject interactBox;
// Use this for initialization
void Start () {
// 获取手柄的引用
trackedOjbect = GetComponent ();
device = SteamVR_Controller.Input ((int)trackedOjbect.index);
}
// Update is called once per frame
void Update () {
if (device == null)
return;
if (device.GetAxis ().x != 0 || device.GetAxis ().y != 0) {
Debug.Log (device.GetAxis ().x+" | "+device.GetAxis ().y);
}
/// 获取Trigger 是否按下
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) {
Debug.Log ("Trigger Down");
if (interactBox != null) {
interactBox.transform.parent = transform;
Rigidbody rig = interactBox.GetComponent ();
rig.useGravity = false;
rig.isKinematic = true;
}
}
/// 获取Trigger 是否松开
if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) {
Debug.Log ("Trigger Up");
if (interactBox != null) {
interactBox.transform.parent = null;
Rigidbody rig = interactBox.GetComponent ();
rig.useGravity = true;
rig.isKinematic = false;
}
}
}
///
/// 碰撞体进入时触发
///
/// Other.
private void OnTriggerEnter(Collider other)
{
Debug.Log ("Enter ");
interactBox = other.transform.gameObject;
}
///
/// 碰撞体离开时触发
///
/// Other.
private void OnTriggerExit(Collider other)
{
Debug.Log ("Exit ");
interactBox = null;
}
private void OnTriggerStay(Collider other)
{
if (device != null) {
/// 手柄震动 单位是毫秒级
device.TriggerHapticPulse (700);
}
}
}
文章来源:https://www.toymoban.com/news/detail-495382.html
文章来源地址https://www.toymoban.com/news/detail-495382.html
以上完成后,点击运行就可以对面前的物体进行操作了。
到了这里,关于Unity-SteamVR物体交互的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!