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

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

相关推荐
敲代码的 蜡笔小新3 小时前
【行为型之观察者模式】游戏开发实战——Unity事件驱动架构的核心实现策略
观察者模式·unity·设计模式·c#
向宇it3 小时前
【unity游戏开发——编辑器扩展】使用EditorGUI的EditorGUILayout绘制工具类在自定义编辑器窗口绘制各种UI控件
开发语言·ui·unity·c#·编辑器·游戏引擎
qq_205279056 小时前
unity 鼠标更换指定图标
unity·游戏引擎
FAREWELL000759 小时前
Unity基础学习(九)输入系统全解析:鼠标、键盘与轴控制
学习·unity·c#·游戏引擎
敲代码的 蜡笔小新13 小时前
【行为型之策略模式】游戏开发实战——Unity灵活算法架构的核心实现策略
unity·设计模式·c#·策略模式
Flamesky1 天前
Unity编辑器重新编译代码
unity·重新编译
qq_5982117572 天前
Unity.UGUI DrawCall合批笔记
笔记·unity·游戏引擎
Tech Synapse2 天前
Unity ML-Agents实战指南:构建多技能游戏AI训练系统
人工智能·游戏·unity
咩咩觉主3 天前
c#数据结构 线性表篇 非常用线性集合总结
开发语言·数据结构·unity·c#·游戏引擎·程序框架
浅陌sss3 天前
Unity中AssetBundle使用整理(一)
unity·游戏引擎