Search Unity

Samsung Gear VR - Detect Tap/Swipe

Discussion in 'AR/VR (XR) Discussion' started by Hammo, Feb 16, 2015.

  1. Hammo

    Hammo

    Joined:
    Apr 8, 2013
    Posts:
    6
    Hi Guys,

    First time Unity user, so please be kind!

    I have ordered and received the Samsung Gear VR and have setup my dev environment and am able to create a project and deploy to the Gear VR for testing.

    One thing I am stuck on at the moment and have not been able to find any simple tutorial or posts is how to detect a tap or swipe on the Gear VR touch pad.

    I've tried using...

    Code (csharp):
    1.  
    2. if (Input.touchCount > 0 ) {
    3.    // Do something
    4. }
    Within the Update method, but nothing at all seams to be detected.

    Can anyone tell me how it should be done for Samsung Gear VR ?

    Thanks
     
    Last edited by a moderator: Feb 16, 2015
  2. Aurore

    Aurore

    Head of Learn Content Production Unity Technologies

    Joined:
    Aug 1, 2012
    Posts:
    3,104
    Hi and welcome, I've moved this to VR, please post in the correct forum next time and use code tags to help others read your code.
     
  3. Hammo

    Hammo

    Joined:
    Apr 8, 2013
    Posts:
    6
    Will do, Thanks.
     
  4. Hammo

    Hammo

    Joined:
    Apr 8, 2013
    Posts:
    6
    I found the solution with help from "MikeF"...Big thanks mate...So for those coming after me, here is the solution.

    You need to correctly map the input before you can code against it in Unity. Go to Edit->Project Settings->Input. This will open the Input Mapping inspector. Make sure you have a Mapping that looks like this...(I named mine "Tap")...

    Mapping.jpg

    You can then access the "Tap" from within your Objects attached script Update Method like this...

    Code (csharp):
    1.  
    2. function Update(){
    3.  
    4.     if(Input.GetButton("Tap"))
    5.       {
    6.       // Do something if tap starts
    7.       }
    8.  
    9.    if(Input.GetButtonUp("Tap"))
    10.       {
    11.       // Do something if tap ends
    12.       }
    13.  
    14. }
    15.  
     
  5. rameshp

    rameshp

    Joined:
    Feb 18, 2015
    Posts:
    10
    Wow thanks this is really helpful. I am a brand new user to Unity myself. Could you please let me know where and how do you attach the above code ?.
     
  6. Hammo

    Hammo

    Joined:
    Apr 8, 2013
    Posts:
    6
    Hi Rameshp, I'm a new user too, so my advice might not be perfect but here goes...You should create a new JS component and paste this code into it. Once that is done, drag and drop that JS code file onto your game object. Your game object, every time it fires the Update' function, will check for the tap and tap up events. Hope that helps.
     
  7. rameshp

    rameshp

    Joined:
    Feb 18, 2015
    Posts:
    10
    Ok so let me see I got this. I have a simple NGUI panel with a button. So do I drag and drop the above script file onto the panel or the button ?. I want the user to be able to tap the button on the panel using the touchpad.

    Currently my ui looks like below Capture.JPG
     
  8. Hammo

    Hammo

    Joined:
    Apr 8, 2013
    Posts:
    6
    Hmnn...that's outside my experience at this point. Although my suggestion should detect a tap start and end, it will do it scene wide. In other words, I believe that if you attach that code to the button, then it won't matter where the user is looking or which button is currently highlighted, as soon as the tap is detected the button with the code attached will detect a tap even if the user is looking at another button, or another button is highlighted. You'll need someone with NGUI experience to solve this one I'm afraid.
     
  9. rameshp

    rameshp

    Joined:
    Feb 18, 2015
    Posts:
    10
    Man I really need to get this right ?. Any pointers ?. Anyone ?. :(:(:(:(:(:(:(
     
  10. Hammo

    Hammo

    Joined:
    Apr 8, 2013
    Posts:
    6
    I have read a little about RayCasting...and I think it MIGHT be possible to determine if the button is in the center of the direction that the main camera is looking. In other words, when the attached script detects a tap, you could then include some code in the same function to check if the user is looking directly at the button or not. If they are, then you perform whatever function you want the button to do. Sorry I can't be much more help...but it might be worth checking out raycasting.
     
  11. rameshp

    rameshp

    Joined:
    Feb 18, 2015
    Posts:
    10
    Ok thanks ...I think that is a good pointer. I will check it out.
     
  12. rameshp

    rameshp

    Joined:
    Feb 18, 2015
    Posts:
    10
    I was finally able to use Raycasting to select a single button by using the user's gaze. It was there in the Unity Integration SDK examples all along. you just need to refactor it to suit your needs. A snippet would look like below.

    Code (csharp):
    1.  
    2.  
    3. Ray ray = new Ray();
    4.  
    5. if(cameraController.GetCameraRay(ref ray)) {
    6.  
    7. // in the editor use the mouse
    8. /*
    9. Camera currentCamera = null;
    10. cameraController.GetCamera( ref currentCamera );
    11. ray = currentCamera.ScreenPointToRay( Input.mousePosition );
    12. */
    13.  
    14. //drop the 'VrButton' script onto a button gameobject
    15. VrButton lastActiveButton = activeButton;
    16. activeButton = null;
    17. RaycastHit hit = new RaycastHit();
    18.  
    19. for (int i = 0; i < buttons.Length; i++)
    20. {
    21. if (buttons[i].collider.Raycast(ray, out hit, 100.0f))
    22. {
    23. activeButton = buttons[i];
    24. if (activeButton != lastActiveButton)
    25. {
    26. Debug.Log("You're Looking at the button : " + activeButton.commandId);
    27.  
    28. PlaySound(vrbuttonSelectSound);
    29. PlayAnim("Button");
    30.  
    31. // activeButton.GetComponent<VrButton>().defaultColor = Color.black;
    32.  
    33. }
    34. break;
    35. }
    36. }
    37.  
    revert back for any issues or if you need more clarifications. And thanks again for the great pointer btw :)
     
    Last edited: Mar 5, 2015
  13. Bonahona

    Bonahona

    Joined:
    Jan 23, 2015
    Posts:
    4
    The touch pad on the Gear VR seems to mapped up as a mouse device.
    This means that you can use normal mouse input code it.

    Input.GetMouseButton(0) will return true if you have a finger on the touchpad and otherwise false.
    Input.GetAxis also works for the Gear Vr touchpad. It correctly maps its x- and y-axis to the Mouse X and Mouse Y axis.

    With this input, it's quite easy to write your own implementation for the simple gestures (Tap, doubletap, Swipe in different direction)

    Input.touches does not work per default. Im not aware if this can be fixed with some configuraion.
     
  14. rijkens

    rijkens

    Joined:
    Dec 17, 2014
    Posts:
    14
    Hi rameshp,

    Thanks for your answer. I also found that part of the script in the SDK

    Can you give a full example of how to use the Raycasting script on a game object? Would be great. Furthermore I cannot find the VrButton script. Would also be great if you can share this!

    gr. ,

    Adriaan
     
  15. AbelSoul

    AbelSoul

    Joined:
    Aug 15, 2015
    Posts:
    1
    This is a bit late but it might help someone.

    I've just managed to customise the Roll a Ball tutorial* to respond to swipes as I don't have a controller yet.
    I replaced:
    Code (csharp):
    1.  
    2.     floatmoveHorizontal = Input.GetAxis ("Horizontal");
    3.     floatmoveVertical = Input.GetAxis ("Vertical");
    4.  
    with:
    Code (csharp):
    1.  
    2.     floatmoveHorizontal = Input.GetAxis ("Mouse X");
    3.     floatmoveVertical = Input.GetAxis ("Mouse Y");
    4.  
    *https://developer.oculus.com/documentation/unity/latest/concepts/unity-tutorial-rollaball-intro/
     
    Last edited: Aug 15, 2015
    tomhewitt, agneshoving and gerosan like this.
  16. vladibo

    vladibo

    Joined:
    Jan 29, 2013
    Posts:
    36
    Do not do it yourself, use Oculus API

    https://forums.oculus.com/viewtopic.php?t=18123

    Code (CSharp):
    1. // on start
    2. OVRTouchpad.Create();
    3. OVRTouchpad.TouchHandler += HandleTouchHandler;
    4.  
    5.    void HandleTouchHandler (object sender, System.EventArgs e)
    6.    {
    7.       OVRTouchpad.TouchArgs touchArgs = (OVRTouchpad.TouchArgs)e;
    8.       if(touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap)
    9.       {
    10.          //TODO: Insert code here to handle a single tap.  Note that there are other TouchTypes you can check for like directional swipes, but double tap is not currently implemented I believe.
    11.       }
    12.    }
     
    Mintah, Jan Rabe, NGC6543 and 3 others like this.
  17. alekz1k

    alekz1k

    Joined:
    Dec 14, 2014
    Posts:
    10
    Hello,

    I cant figure out why nothing happens in the game when i tap on the gear vr.
    I also tried other options such as suggested on the post above without results...
    I know it is a dumb thing, anyone to show me the way?
    I pretty much followed the youtube tutorial by everydayvr and runs Unity5.1.2f1.


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MouseTap : MonoBehaviour {
    6.  
    7.         void Update() {
    8.         if (Input.GetMouseButton(0) || Input.GetButtonDown("Fire1") || Input.GetKeyDown(KeyCode.Space))
    9.             Application.LoadLevel("01");}
    10. }
     
  18. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    ^^ I'm having the same issue as alekz1k.
     
  19. alekz1k

    alekz1k

    Joined:
    Dec 14, 2014
    Posts:
    10
    Hello Hilm,

    I ended up not following the tutorial, not importing the sdk mobile in my unity project and not doing what is mentioned on the forums, and it worked! I still dont get it, but it works.

    I'm now on Unity5.2.2f1
    Basically, dont import any sdk for mobile, just copy your Oculus signature file in your android/asset folder once you switched platform to export on Android in the 'build settings' window, and build. It should work. Let me know how it goes!
    and Happy holidays!
     
  20. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Ahh I'll give that a go! Cheers dude.
     
  21. makapiatia

    makapiatia

    Joined:
    Apr 4, 2016
    Posts:
    1
    alekz1k likes this.
  22. asianguyenduy

    asianguyenduy

    Joined:
    Aug 15, 2016
    Posts:
    2
    having trouble copying the GearVRInput.cs script into the OVRPlayerController.cs script

    when i paste to line 222 of OVRPlayerController.cs as specified i get the error messages stating "Cannot use local variable 'moveForward' before it is declared"
    and
    "A local variable named 'moveBack' is already defined in this scope"

    was wondering if anyone could help me out?