make controller vibrate and 判断是否grab

我自己的例子,新建cube上挂载oculus交互的代码,如下

然后加载自己写的代码到cube上就可以了

cs 复制代码
using Oculus.Interaction.HandGrab;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Vibtation : MonoBehaviour
{
    //private OVRGrabbable handgrab;
   
    // Start is called before the first frame update
    void Start()
    {
        
        // handgrab = GetComponent<OVRGrabbable>();
        
    }

    // Update is called once per frame
    void Update()
    {
        //if (handgrab.isGrabbed)
        //{
        //    OVRInput.SetControllerVibration(1, 1, OVRInput.Controller.RTouch);
        //    OVRInput.SetControllerVibration(1, 1, OVRInput.Controller.LTouch);
        //}
        if (OVRInput.Get(OVRInput.RawButton.RIndexTrigger)&& OVRInput.Get(OVRInput.RawButton.RHandTrigger))
        {
            OVRInput.SetControllerVibration(10, 1, OVRInput.Controller.RTouch);
            
        }
        if (OVRInput.Get(OVRInput.RawButton.LIndexTrigger) && OVRInput.Get(OVRInput.RawButton.LHandTrigger))
        {
            OVRInput.SetControllerVibration(1, 0.5f, OVRInput.Controller.LTouch);
        }

    }
}

震动之后如果松手了,不会立即停止震动,会延迟两三秒钟,为了更好的效果,当然需要精益求精了。每个if后面加一个else震动强度设置成0,处理松开手的代码就好了。经测试效果很好。

cs 复制代码
if (OVRInput.Get(OVRInput.RawButton.RIndexTrigger) && OVRInput.Get(OVRInput.RawButton.RHandTrigger))
                {
                    //Debug.Log("Trigger detected ******************");
                    OVRInput.SetControllerVibration(10, 1, OVRInput.Controller.RTouch);
                   
                }
                else
                    OVRInput.SetControllerVibration(10, 0, OVRInput.Controller.RTouch);

                if (OVRInput.Get(OVRInput.RawButton.LIndexTrigger) && OVRInput.Get(OVRInput.RawButton.LHandTrigger))
                {
                    OVRInput.SetControllerVibration(10, 1, OVRInput.Controller.LTouch);
                }
                else
                    OVRInput.SetControllerVibration(10, 0, OVRInput.Controller.LTouch);

以下是以前的参考而已.

This is a simple way to make the controller vibrate when shooting a gun.

Just call Vib().

You can change the length of the vibrate by changing the time variables in Invoke.

How to make Quest 2 controller vibrate when shooting a gun. - Unity Engine - Unity Discussions

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Buzz : MonoBehaviour
{
    public float time = 3f;
    // Start is called before the first frame update


    // Update is called once per frame
    void Update()
    {
       
    }
    public void Vib()
    {
        Invoke("startVib", .1f);
        Invoke("stopVib", .4f);
    }
    public void startVib()
    {
        OVRInput.SetControllerVibration(1, 1, OVRInput.Controller.RTouch);
    }
    public void stopVib()
    {
        OVRInput.SetControllerVibration(0, 0, OVRInput.Controller.RTouch);
    }
}

另外判断是否grab了的句子如下:https://www.youtube.com/watch?v=9KJqZBoc8m4

(经测试无法用到手抓上边)

相关推荐
两水先木示2 小时前
【Unity3D】微信小游戏适配安全区域或胶囊控件(圆圈按钮)水平高度一致方案
unity·微信小游戏·安全区域·ui适配·胶囊控件·safearea
枯萎穿心攻击3 小时前
ECS由浅入深第三节:进阶?System 的行为与复杂交互模式
开发语言·unity·c#·游戏引擎
不绝1914 小时前
怪物机制分析(有限状态机、编辑器可视化、巡逻机制)
网络·游戏·unity·游戏引擎
unicrom_深圳市由你创科技4 小时前
Unity开发如何解决iOS闪退问题
unity·ios·蓝桥杯
Yasin Chen9 小时前
C# Dictionary源码分析
算法·unity·哈希算法
深海潜水员17 小时前
【Behavior Tree】-- 行为树AI逻辑实现- Unity 游戏引擎实现
游戏·unity·c#
Thomas_YXQ1 天前
Unity3D游戏内存优化指南
游戏·unity·职场和发展·性能优化·蓝桥杯·游戏引擎·unity3d
chillxiaohan1 天前
Unity接入Steamworks.NET实现通信功能
unity
枯萎穿心攻击2 天前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
X_StarX2 天前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生