我自己的例子,新建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
(经测试无法用到手抓上边)