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

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

相关推荐
君莫愁。18 小时前
【Unity】搭建基于字典(Dictionary)和泛型列表(List)的音频系统
数据结构·unity·c#·游戏引擎·音频
唐小墨同学1 天前
Pico 4 Enterprise(企业版)与Unity的交互-打包运行及UI交互篇
ui·unity
红黑色的圣西罗1 天前
Unity UGUI下优化需要射线检测类的UI元素的一种方式
unity·游戏引擎
Thomas_YXQ1 天前
Unity3D 图形渲染(Graphics & Rendering)详解
开发语言·unity·图形渲染·unity3d·shader
类人_猿2 天前
Unity自定义区域UI滑动事件
ui·unity·游戏引擎·滑动事件
iCan_qi2 天前
【AI】【Unity】关于Unity接入DeepseekAPI遇到的坑
unity·ai·游戏引擎
_清风来叙2 天前
【Unity】改变游戏运行时Window的窗口标题
游戏·unity·游戏引擎
吴梓穆3 天前
unity pico开发 四 物体交互 抓取 交互层级
unity
先生沉默先3 天前
3dsmax烘焙光照贴图然后在unity中使用
unity·3dsmax·贴图