HTC-VIVE手柄使用代码

2023-11-19

using UnityEngine;  
using System.Collections;  
//检测手柄功能的脚本 这个脚本挂到手柄上(controler(right)和controler(left))上  
public class ButtonTouchAction : MonoBehaviour {  
    //手柄  
    SteamVR_TrackedObject trackdeObjec;  

    void Awake() {  
        //获取手柄上的这个组件  
        trackdeObjec = GetComponent<SteamVR_TrackedObject>();  
    }  
    // Use this for initialization  
    void Start () {     
    }  
    void FixedUpdate()  
    {   //获取手柄输入  
        var device = SteamVR_Controller.Input((int)trackdeObjec.index);  
        //以下是api中复制出来的按键列表  
        /*       public class ButtonMask 
           { 
               public const ulong System = (1ul << (int)EVRButtonId.k_EButton_System); // reserved 
               public const ulong ApplicationMenu = (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu); 
               public const ulong Grip = (1ul << (int)EVRButtonId.k_EButton_Grip); 
               public const ulong Axis0 = (1ul << (int)EVRButtonId.k_EButton_Axis0); 
               public const ulong Axis1 = (1ul << (int)EVRButtonId.k_EButton_Axis1); 
               public const ulong Axis2 = (1ul << (int)EVRButtonId.k_EButton_Axis2); 
               public const ulong Axis3 = (1ul << (int)EVRButtonId.k_EButton_Axis3); 
               public const ulong Axis4 = (1ul << (int)EVRButtonId.k_EButton_Axis4); 
               public const ulong Touchpad = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Touchpad); 
               public const ulong Trigger = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Trigger); 
           } 
           */  

           //同样是三种按键方式,以后不做赘述  
        if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)) {  
            Debug.Log("按了 “trigger” “扳机键”");  

            //右手震动  
            //拉弓类似操作应该就是按住trigger(扳机)gettouch时持续调用震动方法模拟弓弦绷紧的感觉。  
            var deviceIndex2 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);  
            SteamVR_Controller.Input(deviceIndex2).TriggerHapticPulse(500);  

        }  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))  
        {   Debug.Log("按下了 “trigger” “扳机键”");  

        }  
        if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)) {  
            Debug.Log("松开了 “trigger” “扳机键”");  

            //左手震动  
            var deviceIndex = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost);  
            SteamVR_Controller.Input(deviceIndex).TriggerHapticPulse(3000);  

            //右手震动  
            var deviceIndex1 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);  
            SteamVR_Controller.Input(deviceIndex1).TriggerHapticPulse(3000);  
        }  

        //这三种也能检测到 后面不做赘述  
        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” “扳机键”");  
        }  

        //system键 圆盘下面那个键   
        // reserved 为Steam系统保留,用来调出Steam系统菜单 因此貌似自己加的功能没啥用  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.System))  
        {  
            Debug.Log("按下了 “system” “系统按钮/Steam”");  
        }  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.System))  
        {  
            Debug.Log("用press按下了 “System” “系统按钮/Steam”");  
        }  

        //ApplicationMenu键 带菜单标志的那个按键(在方向圆盘上面)  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu))  
        {  
            Debug.Log("按下了 “ApplicationMenu” “菜单键”");  
        }  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))  
        {  
            Debug.Log("用press按下了 “ApplicationMenu” “菜单键”");  
        }  

        //Grip键 两侧的键 (vive雇佣兵游戏中的换弹键),每个手柄左右各一功能相同,同一手柄两个键是一个键。  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Grip))  
        {  
            Debug.Log("按下了 “Grip” “ ”");  
        }  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))  
        {  
            Debug.Log("用press按下了 “Grip” “ ”");  
        }  



        //Axis0键 与圆盘有交互 与圆盘有关  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))  
        {  
            Debug.Log("按下了 “Axis0” “方向 ”");  
        }  
        //按动触发  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))  
        {  
            Debug.Log("用press按下了 “Axis0” “方向 ”");  
        }  

        //Axis1键  目前未发现按键位置  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1))  
        {  
            Debug.Log("按下了 “Axis1” “ ”");  
        }  
        //按动触发   
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1))  
        {  
            Debug.Log("用press按下了 “Axis1” “ ”");  
        }  

        //Axis2键 目前未发现按键位置  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2))  
        {  
            Debug.Log("按下了 “Axis2” “ ”");  
        }  
        //按动触发  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2))  
        {  
            Debug.Log("用press按下了 “Axis2” “ ”");  
        }  

        //Axis3键  未目前未发现按键位置  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3))  
        {  
            Debug.Log("按下了 “Axis3” “ ”");  
        }  
        //按动触发  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3))  
        {  
            Debug.Log("用press按下了 “Axis3” “ ”");  
        }  

        //Axis4键  目前未发现按键位置  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4))  
        {  
            Debug.Log("按下了 “Axis4” “ ”");  
        }  
        //按动触发  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4))  
        {  
            Debug.Log("用press按下了 “Axis4” “ ”");  
        }  

<pre name="code" class="csharp">       //方向圆盘:  
        //这里开始区分了press检测与touch检测的不同之处,圆盘可以触摸,顾名思义,touch检测的是触摸,press检测的是按动<pre name="code" class="csharp"> //Axis0键 与圆盘有交互 与圆盘有关  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))  
        {  
            Debug.Log("按下了 “Axis0” “方向 ”");  
        }  
        //按动触发  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))  
        {  
            Debug.Log("用press按下了 “Axis0” “方向 ”");  
        }  

        //Axis1键  目前未发现按键位置  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1))  
        {  
            Debug.Log("按下了 “Axis1” “ ”");  
        }  
        //按动触发   
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1))  
        {  
            Debug.Log("用press按下了 “Axis1” “ ”");  
        }  

        //Axis2键 目前未发现按键位置  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2))  
        {  
            Debug.Log("按下了 “Axis2” “ ”");  
        }  
        //按动触发  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2))  
        {  
            Debug.Log("用press按下了 “Axis2” “ ”");  
        }  

        //Axis3键  未目前未发现按键位置  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3))  
        {  
            Debug.Log("按下了 “Axis3” “ ”");  
        }  
        //按动触发  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3))  
        {  
            Debug.Log("用press按下了 “Axis3” “ ”");  
        }  

        //Axis4键  目前未发现按键位置  
        //触摸触发  
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4))  
        {  
            Debug.Log("按下了 “Axis4” “ ”");  
        }  
        //按动触发  
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4))  
        {  
            Debug.Log("用press按下了 “Axis4” “ ”");  
        }  


        //ATouchpad键 圆盘交互  
        //触摸触发  
        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” “ ”");  
        }  





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

    }  
    //方向圆盘最好配合这个使用 圆盘的.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;  
    }  
}  

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HTC-VIVE手柄使用代码 的相关文章

  • 【华为OD机试真题2023B卷 JAVA&JS】字符串分割

    华为OD2023 B卷 机试题库全覆盖 刷题指南点这里 字符串分割 时间限制 3秒 内存限制 262144K 语言限制 不限 题目描述 给定非空字符串s 将该字符串分割成一些子串 使每个子串的ASCII码值的和均为水仙花数 1 若分割不成功

随机推荐

  • 4.函数、模块与包

    文章目录 一 函数 二 模块与包 引用 一 函数 Python 使用 def 关键字来声明函数 格式如下所示 def 函数名 参数 函数体 return 返回值 如果要定义一个无任何功能的空函数 函数体只写 pass 即可 def 函数名
  • element组件库中tab表格利用cellStyle方法单独改变单元格样式

    公司项目中已经封装了一个公共的tab组件 新的需求需要对表格中特定的单元格字体颜色做改变 查询了官方文档发现有cellStyle这个属性 在公共组件中加了标签属性 一开始是使用 emit方法触发父传子 使用该表格是自定义该事件 但是样式不生
  • 最大似然估计【MLE】与最大后验概率【MAP】

    最大似然估计 Maximum likelihood estimation 简称MLE 和最大后验概率估计 Maximum a posteriori estimation 简称MAP 是很常用的两种参数估计方法 如果不理解这两种方法的思路 很
  • matlab 回归

    我发现这两天写题目 回归真的是个万能方法 但是我只会最简单的线性回归 为此特地记录一下以下几种方法 1 regress 简单线性回归 可以是一元 也可以是多元 具体用法可以看这个图片 这个方法最简单 也最好用 但是也有局限 比如非线性的时候
  • Unity与Andriod交互错误合集

    一 无法调用安卓中的方法no non static method with name 报错如下 在保证代码中的方法名没有问题 并且调用的方法名的返回值和传递的参数等都没有问题的情况下 第一 查看在Unity项目中jar包存放的位置是否正确
  • 前言 在学习达梦数据库数据存储过程中有接触到行式存储和列式存储方面的内容 在此作简单的学习分享 通过本文你可以了解到行存储模式 列存储模式 它们的优缺点以及列存储模式的优化等知识 Row vs Column Oriented Databas
  • 怎样用计算机的计算器的程序员进行进制,使用系统自带计算器进行二进制运算(示例代码)...

    int x 110 int y 10 Console WriteLine x y Console WriteLine x y 想亲自算一下这种计算的时候 打开windows自带的计算器calc exe 调到 程序员计算器 模式即可 选择DE
  • 华为OD机试 - 日志限流(Java )

    题目描述 某软件系统会在运行过程中持续产生日志 系统每天运行N单位时间 运行期间每单位时间产生的日志条数保行在数组records中 records i 表示第i单位时间内产生日志条数 由于系统磁盘空间限制 每天可记录保存的日志总数上限为to
  • 详解人工智能领域重大突破:GPT-3

    2020 09 14 16 09 导语 GPT 3是自然语言处理 领域迄今为止发布出来最大的Transformer模型 超过之前的记录 微软研究院Turing LG的170亿参数 约10倍 英语原文 Exploring GPT 3 A Ne
  • 剑指 Offer 18. 删除链表的节点 -- 双指针

    0 题目描述 leetcode原题链接 剑指 Offer 18 删除链表的节点 1 双指针解法 删除值为 val 的节点分需为两步 定位节点 修改引用 定位节点 遍历链表 直到 head val val 时跳出 即可定位目标节点 修改引用
  • 荐读

    本文转自 链闻 ChainNews 作者 Karen 虽然区块链技术诞生至今不过短短十余年 但是东西方天然的文化差异在加密世界中同样留下了一些痕迹 当社交媒体上关于 东方区块链 只关心币价 利益和投机而只有 西方区块链 才关心底层技术的革新
  • “元宇宙”既是机遇也是挑战

    一次性看懂元宇宙 是开疆拓土还是新一轮割韭菜 众所周知 随着AI VR AR MR XR 区块链 云计算 物联网 数字孪生 量子技术 5G技术等新兴技术的快速发展 推动刚刚过去的2021年成为元宇宙元年 先是3月Robolox上市 开启了元
  • 基于Dpabi的功能连接

    1 预处理 这里预处理用Gretna软件进行 共分为以下几步 1 DICOM转NIfTI格式 2 去除前10个时间点 Remove first 10 times points 由于机器刚启动 被试刚躺进去也还需适应环境 导致刚开始扫描的数据
  • 分享一个开源免费、功能强大的视频播放器库

    99 的前端开发者都关注了这个公众号 点击上方 前端开发博客 关注并 设为星标 回复加群 自助秒进前端群 最近在开发一个前端项目 用到播放视频的功能 所以就查了下有什么前端的视频播放器库可以使用 今天来分享一下给大家 这个库的名字叫做 Pl
  • Zabbix之自定义监控MySQL主从状态和延迟

    zabbix之自定义监控MySQL主从状态和延迟 文章目录 监控MySQL主从状态 1 安装Mariadb配置主从 2 配置监控脚本 3 web界面添加监控项 4 添加触发器 5 触发验证 监控MySQL主从延迟 配置监控脚本 web界面添
  • 机器学习-knn近邻分类算法

    算法原理 本质是通过距离 欧式距离 判断两个样本是否相似 如果距离够近就认为他们足够相似属于同一类别 算法优缺点 主要参数k 标记数据周围几个数作为参考对象 需要根据数据来决定 k值越大 模型偏差大 对噪声数据不敏感 可能造成欠拟合 k值越
  • 若依文件下载

    若依文件下载 都看我的 2021 4 17 找了一圈 每一个写的简单的 还得自己完成 提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 前言 一 前台代码 二 加入js代码 总结 前言 提示 若依框架的文件下载 提
  • css 让内容可滑动,css实现隐藏滚动条并可以滚动内容

    代码预览 行走在光阴里的人 谁不对初见怀揣一份美好向往和期待 谁不对初见心存一份眷恋和不舍 假如人生是一场途经 初见一定是人生路上最美的绽放 人生在世 不管你是青丝如云 还是白发如霜 当你念及 人生若只如初见 时 你的嘴角一定会不由自主地泛
  • ChatGTP套壳网站总结更新

    总结一批ChatGTP套壳网站供大家学习参考 前10个网站经过测试可用 所以套壳网站 就是使用ChatGPT提供的API与ChatGPT系统连接 使用自己的网站来实现交互展示 效果与在ChatpGPT网站上聊天是一样的 ChatGTP套壳网
  • HTC-VIVE手柄使用代码

    using UnityEngine using System Collections 检测手柄功能的脚本 这个脚本挂到手柄上 controler right 和controler left 上 public class ButtonTouc