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

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

相关推荐
Thomas_YXQ6 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
KhalilRuan10 小时前
Unity-MMORPG内容笔记-其一
unity·游戏引擎
向宇it14 小时前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
切韵10 天前
Unity编辑器扩展:UI绑定复制工具
ui·unity·编辑器
11 天前
Lua复习之何为闭包
开发语言·unity·游戏引擎·lua·交互
深空数字孪生11 天前
2025年小程序地图打车的5大技术革新:实时路况预测与智能调度升级
大数据·人工智能·unity·性能优化·小程序·游戏引擎
程序猿多布11 天前
Unity Addressable使用之检测更新流程
unity·addressable
Bunny Chen12 天前
Unity中的物理单位是真实的吗?
unity·游戏引擎
benben04412 天前
Unity3D仿星露谷物语开发69之动作声音
游戏·ui·unity·c#·游戏引擎
徐子竣12 天前
Unity Shader开发-着色器变体(1)-着色器变体概述
unity·游戏引擎·着色器