Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by abragon · Jul 08, 2017 at 09:10 PM · c#vroculushaptic

how to implement oculus vr haptic feedback

I want to implement haptic feedback for our oculus vr game, but I don't understand how. I went to the documentation which was confusing. A lot of those types aren't recognised. The following code sample is provided by the document.

 uint8_t amplitude = (uint8_t)round(handTrigger[t] * 255);
 
     result = ovr_GetControllerVibrationState(Session, touchController[t], &state);
     if (result != ovrSuccess || state.SamplesQueued >= kLowLatencyBufferSizeInSamples)
     {
         DefaultChannel.LogWarningF("%s Haptics skipped. Queue size %d", kTouchStr[t], state.SamplesQueued);
         continue;
     }
 
     for (int32_t i = 0; i < kLowLatencyBufferSizeInSamples; ++i)
         samples.push_back(amplitude);
 
     if (samples.size() > 0)
     {
         ovrHapticsBuffer buffer;
         buffer.SubmitMode = ovrHapticsBufferSubmit_Enqueue;
         buffer.SamplesCount = (uint32_t)samples.size();
         buffer.Samples = samples.data();
         result = ovr_SubmitControllerVibration(Session, touchController[t], &buffer);
         if (result != ovrSuccess)
         {
             // Something bad happened
             DefaultChannel.LogErrorF("%s: Haptics submit failed %d", kTouchStr[t], result);
         }
     }

Could someone please explain to me how to implement this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by HomerDalors · Jul 25, 2017 at 11:17 AM

This snippet might help:

 public enum VibrationForce
 {
     Light,
     Medium,
     Hard,
 }
 
 
 public class OculusHaptics : MonoBehaviour
 {
 
     [SerializeField]
     OVRInput.Controller controllerMask;
 
     private OVRHapticsClip clipLight;
     private OVRHapticsClip clipMedium;
     private OVRHapticsClip clipHard;
 
     public  float lowViveHaptics { get; private set; }
     public  float mediumViveHaptics { get; private set; }
     public float hardViveHaptics { get; private set; }
 
     
     private void Start()
     {
         InitializeOVRHaptics();
     }
 
     private void InitializeOVRHaptics()
     {
         
         int cnt = 10;
         clipLight = new OVRHapticsClip(cnt);
         clipMedium = new OVRHapticsClip(cnt);
         clipHard = new OVRHapticsClip(cnt);
         for (int i = 0; i < cnt; i++)
         {
             clipLight.Samples[i] = i % 2 == 0 ? (byte)0 : (byte)45;
             clipMedium.Samples[i] = i % 2 == 0 ? (byte)0 : (byte)100;
             clipHard.Samples[i] = i % 2 == 0 ? (byte)0 : (byte)180;
         }
 
         clipLight = new OVRHapticsClip(clipLight.Samples, clipLight.Samples.Length);
         clipMedium = new OVRHapticsClip(clipMedium.Samples, clipMedium.Samples.Length);
         clipHard = new OVRHapticsClip(clipHard.Samples, clipHard.Samples.Length);
     }
 
 
     void OnEnable()
     {
         InitializeOVRHaptics();
     }
 
     public void Vibrate(VibrationForce vibrationForce)
     {
         var channel = OVRHaptics.RightChannel;
         if (controllerMask == OVRInput.Controller.LTouch)
             channel = OVRHaptics.LeftChannel;
 
         switch (vibrationForce)
         {
             case VibrationForce.Light:
                 channel.Preempt(clipLight);
                 break;
             case VibrationForce.Medium:
                 channel.Preempt(clipMedium);
                 break;
             case VibrationForce.Hard:
                 channel.Preempt(clipHard);
                 break;
         }
     }
 
 
 
     public IEnumerator VibrateTime(VibrationForce force, float time)
     {
         forcedHaptic = true;
         var channel = OVRHaptics.RightChannel;
         if (controllerMask == OVRInput.Controller.LTouch)
             channel = OVRHaptics.LeftChannel;
 
         for (float t = 0; t <= time; t += Time.deltaTime)
         {
             switch (force)
             {
                 case VibrationForce.Light:
                     channel.Queue(clipLight);
                     break;
                 case VibrationForce.Medium:
                     channel.Queue(clipMedium);
                     break;
                 case VibrationForce.Hard:
                     channel.Queue(clipHard);
                     break;
             }
         }
         yield return new WaitForSeconds(time);
         channel.Clear();
         forcedHaptic = false;
         yield return null;
 
     }

Attach it to the Controller Anchor and set it accordingly (Ltouch or RTouch), then just call the methods Vibrate or the coroutine VibrateTime.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abragon · Jul 25, 2017 at 08:14 PM 0
Share

Thank you for answering! This looks like really good, but I can't test it right now. In two weeks I get my hands on a Oculus again, I will test it then and come back here, if that's okey.

avatar image abragon · Aug 14, 2017 at 08:50 PM 0
Share

Thanks for sharing! They should add this to the docs as an example, this is something I wouldn't have figured out by myself. I got one point, the bool forcedHaptic was never declared.

avatar image HomerDalors · Aug 24, 2017 at 04:36 PM 0
Share

Opss, sorry. the forcedHaptic bool was an artifact in our project. it is irrelevant in this snippet sorry.

avatar image DavidT98 · Mar 28, 2018 at 08:28 PM 0
Share

Thank you so much sir!

avatar image sgrein · Oct 18, 2018 at 03:00 AM 0
Share

Hello, using this code I get a Divison by Zero exception:

 IndexOutOfRangeException: Array index is out of range.
 VibrationBehaviour.InitializeOVRHaptics () (at Assets/VibrationBehaviour.cs:42)
 VibrationBehaviour.OnEnable () (at Assets/VibrationBehaviour.cs:55)

$$anonymous$$y code line 42 corresponds to Line 39 in the code posted above.

Any idea why this happens?

Best, Stephan

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

367 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Draw texture directly on Oculus screen 1 Answer

Reload a gun in VR with motion controllers 1 Answer

Help importing personalized Oculus avatar 0 Answers

,Does anyone know how to implement the touch controllers on a build made for Samsung Gear? 0 Answers

Why doesn't my VR iron man flight system work? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges