make controller vibrate and 判断是否grab

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

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

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,处理松开手的代码就好了。经测试效果很好。

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

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值