Search Unity

[SOLVED]leap motion play sound only when pinched successfully

Discussion in 'Scripting' started by ralf_b, Feb 9, 2016.

  1. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    Hello everybody,

    hope this is the right subforum to ask this, because its not really about VR but more about C#

    So far I can play a sound whenever a pinch gesture is detected, but I would like the sound to be played only when an object has been succesfully grabbed.

    For pinching I used leap motions magentic pinch script which you can find here:
    https://github.com/leapmotion-examp...ets/LeapMotion/Scripts/Utils/MagneticPinch.cs
    I have my script not at hand now, but I only added an audio.Play(); anyways.

    Has anybody got a tip on how to acchieve what I am looking for? Right now I think I might add a function which only plays the sound when the pinch gesture has been detected AND one of the pinchable objects is colliding with the leap motion hand controller. But maybe there is an easier way which I have overlooked
     
  2. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    I can't help much as it was a while ago that I did something, but from what I remember, there are pinchStrength and grabStrength members in the SDK. You can alternatively use those, or additionally use them with your code. Playing the sound is the easy part compared to grabbing objects in a convincing manner :S
     
    ralf_b likes this.
  3. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    @crash664 thanks for pointing me in the right direction, although I did it a bit different. Just got back from home and looked again through leap motions magnetic pinch script and while doing so found the right place to start my audio from :D


    Code (CSharp):
    1.    
    2. void OnPinch(Vector3 pinch_position) {
    3.         pinching_ = true;
    4.         // Check if we pinched a movable object and grab the closest one that's not part of the hand.
    5.         Collider[] close_things = Physics.OverlapSphere(pinch_position, magnetDistance);
    6.         Vector3 distance = new Vector3(magnetDistance, 0.0f, 0.0f);
    7.  
    8.         for (int j = 0; j < close_things.Length; ++j) {
    9.             Vector3 new_distance = pinch_position - close_things[j].transform.position;
    10.             if (close_things[j].GetComponent<Rigidbody>() != null && new_distance.magnitude < distance.magnitude &&
    11.                 !close_things[j].transform.IsChildOf(transform)) {
    12.                 // PUT SOUND HERE TO PLAY WHEN GRABBED SUCCESFULLY
    13.                 GetComponent<AudioSource>().Play ();
    14.                 grabbed_ = close_things[j];
    15.                 distance = new_distance;
    16.             }
    17.         }
    18.     }
     
    crash664 likes this.