Search Unity

Input.Touches - Simple Gesture Solution

Discussion in 'Assets and Asset Store' started by Song_Tan, Apr 28, 2012.

  1. JuanCamiloAlcaraz

    JuanCamiloAlcaraz

    Joined:
    Apr 30, 2014
    Posts:
    7
    Hi, I have a doubt, into your package, you have events for start dragging and end dragging, the same for charging, but for pinch, you don´t have it (or I am missin something) called pinch start or pich end... it is possible to create something like this? it is possible to solve this?. I am asking you this, because I want to call a coroutine when I have ended the pinch like I did it with the "onDraggingEndE".

    Thanks.
     
  2. cchute

    cchute

    Joined:
    Aug 10, 2014
    Posts:
    30
    Hi again, I've been intermittently getting this null pointer exception:

    NullReferenceException: Object reference not set to an instance of an object

    IT_Gesture.MultiTap (.Tap tap) (at Assets/Plugins/InputTouches/IT_Gesture.cs:242)

    TapDetector.CheckMultiTapMouse (Int32 index, Vector2 startPos, Vector2 lastPos) (at Assets/Plugins/InputTouches/TapDetector.cs:139)

    TapDetector+<MouseRoutine>c__IteratorB.MoveNext () (at Assets/Plugins/InputTouches/TapDetector.cs:472)



    public static void MultiTap(Tap tap){

    if(tap.fingerCount==1){

    if(tap.count==1){

    if(onShortTapE!=null) onShortTapE(tap.pos);

    }

    else if(tap.count==2){

    if(onDoubleTapE!=null) onDoubleTapE(tap.pos);

    }


    if(instance.enableMultiTapFilter) instance.CheckMultiTap(tap);

    else if(onMultiTapE!=null) onMultiTapE(tap);

    ***second last line it where unity highlights

    Any ideas how to resolve this? It happened when I made a double tap. Thanks
     
  3. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    @JuanCamiloAlcaraz, unfortunately there's no support for pinch-start or pinch-end event yet. But it's not a problem you cant get around yourself. Try something like this:
    Code (csharp):
    1.  
    2.    private bool pinchOn=false;
    3.    void OnPinch(PinchInfo pinchInfo){
    4.      if(!pinchOn){
    5.        pinchOn=true;
    6.        //pinch start
    7.      }
    8.    }
    9.    void Update(){
    10.      if(pinchOn && Input.touchCount!=2){
    11.        pinchOn=false;
    12.        //pinch end
    13.      }
    14.    }


    @cchute, from what I can gather, the problem is probably due to IT_Gesture component is missing in your scene or it's disabled.
     
  4. cchute

    cchute

    Joined:
    Aug 10, 2014
    Posts:
    30
    The IT_Gesture component is there an enabled, any other ideas?
     
  5. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I'm not sure what else could have cause this. Here's a way on how you can possibly fix this. Change line 242 and 255 to:

    if(instance!=null && instance.enableMultiTapFilter) instance.CheckMultiTap(tap); //line242
    if(instance!=null && instance.enableMultiTapFilter) instance.CheckMFMultiTap(tap); //line255


    If the error is what I think it's (that the instance of IT_Gesture is missing), it should eliminate the error. However it didn't really fix the issue so the multiTap filter won't be working.
     
  6. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    Works like a charm!
     
  7. Andrew-Kite

    Andrew-Kite

    Joined:
    Oct 16, 2014
    Posts:
    34
    @songtan
    So far I have setup everything I need with Input.Touches really easy and works great.

    I just have one problem I cannot seem to get passed. I am using Dragging gesture to move my camera around and when I am in editor I manage to get the calculations just right so the speed is not to fast or slow.
    When porting to iPad however the speed of the movement is increased, and is also different on iPhone. I am assuming this would be caused by the devices having different DPI's and was hoping if you could shed some light on whether you plan to accommodate this in a future update?

    My first guess would be to check what generation the device is and make calculation per device, as tedious as it is. But then I would have to update the game for every new device that comes out, not to mention wait for Unity to update their Generation list which who knows how long that could be.

    Any other work around you can think of would be greatly appreciated.
     
  8. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I'm currently working on updating all my package on AssetStore to Unity4.6. Although InputTouches is on the list, it's not on the very top. When I get to it, I'll probaby add in code for auto-dpi detect and compensation. But I'm afraid it will have to wait a bit while I get through the list.

    What you can do for now is try this - http://docs.unity3d.com/ScriptReference/Screen-dpi.html. Might be a lot more easier than what you are currently doing. You can retrieve the value and use it to modify you the drag speed directly in your script. Keep in mind that the value is 0 when the it's not valid, so you might have to add a safety fallback value. Like so:

    Code (csharp):
    1. int dpi=Screen.dpi!=0 ? Screen.dpi : 200,
     
  9. Andrew-Kite

    Andrew-Kite

    Joined:
    Oct 16, 2014
    Posts:
    34
    Awesome, thanks for the clarification.
    Also thanks for the code snippet and pointing me in the right direction!
     
  10. Joelrube

    Joelrube

    Joined:
    Mar 23, 2014
    Posts:
    1
    2 ques.
    Does Input.touches rotate object example work on IOS?
    How do you change the initial Z position of the camera in your rotate object example?
     
  11. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Yes, all the examples should work on iOS touch device.

    Not sure if you are refering to the position of the camera transform (named Main Camera) or the pivot transform of the camera (named CameraParent). The pivot point of the camera is basically locked to the target object's position so you cant really change it unless you change the code. Just disable line72 in the script and you should be able to change the position. If you are refering the the actual position of the camera, just change the value in the inspector will do.
     
  12. DPoli

    DPoli

    Joined:
    May 20, 2013
    Posts:
    37
    Hi,

    Could you upload an Android build with the demo you have available as a Webplayer?

    I want to test it on android before I make a purchase.

    Thank you.
     
  13. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Give me a few minutes to make sure. The UI might not be up to scratch when put on a very small screen. I'll send you a link when it's done.
     
  14. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    @DPoli, I've just pm'ed you the link on an Android build. Anyone else who like a test please let me know.
     
  15. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    Hi Song,

    Is it possible to use your asset to create a scene similar to this asset, where the length of the swipe controls the vertical angle:

    http://www.saadkhawaja.com/demos/?demo=flickstarterkit

    1. But I would also like to have the speed of my finger movement control the Power/Force of the kick.

    2. Also, have the ball curve sideways if I curve the swipe. Is this possible with your asset?

    3. And does your asset automatically adjust for the screen resolution of the target device.

    Is this possible with your asset?

    Thank you!
     
  16. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Let me clarify, InputTouches is a touch gesture detector library that detect specific input and pass you the relevant data, ie, touch position, touch duration, swipe direction and so on. Although it comes with a lot of example, some borderline a working minigame, what have you got with the package essentiatlly is just a mean to get the input information. It's up to you to decide how to use those information and implement the mechanic.

    Now we got that out of the way, the answer to your fist and second question, objectively, is yes. With the information obtain from a swipe, I dont see why some thing like the demo you showed me can't be made.

    As for question3, I take it you mean adjust the input sensitivity based on the device dpi. The answer is not at the moment. But I'm working on an update which I hope to release in a few days time.
     
  17. ZenithCode

    ZenithCode

    Joined:
    Jul 26, 2012
    Posts:
    42
    Hi Songtan,

    Can this be used with NGUI widgets like UISprites and UILabels?
    Thanks.
     
  18. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    It depends. InputTouches will only provide you the raw data input. If you meant to use those data to code your own detection mechanism, then yes, it's doable. But as it's, it wont auto detect input that lands on the UI element.
     
  19. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Hi all, the latest version of InputTouches (v1.2) is now live on AssetStore. The changes are as follow:
    • Added custom editor to all the component, with tooltip explanation for all the variable.
    • Added facility for DPI based input sensitivity scaling
    • Rearrangement of the package to make it tidier
     
  20. Squi_Beta

    Squi_Beta

    Joined:
    Dec 11, 2013
    Posts:
    5
    Hello!
    I just bought your plugin and it's fabulous :)
    I just wanted to ask if it's planned the support for the new GUI system in Unity 4.6, because I'm already using the beta and it's great :D

    Thanks
     
  21. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Thanks for your purchase.

    I dont really have plan to support the new uGUI. I mean, they are not relatable in any manner. InputTouches on it's own doesnt need any UI at all and uGUI can detect touch input just fine with any 3rd party input library.

    Any UI you see in the demo are just to help showcase how the plugin can be used. There's really no need for it to use the latest version as long as it's functional. And I doubt anyone is getting this plugin for the UI.

    If I've misunderstood what you mean, please correct me.
     
  22. Squi_Beta

    Squi_Beta

    Joined:
    Dec 11, 2013
    Posts:
    5
    It's just that one of my big problem with Unity was with the old GUI back then. I didn't really understand how to use it. Now with version 4.6 everything seems easier. I see that the example scenes in the plugin use the old GUI for buttons and everything and I was wondering if the plugin might work with uGUI.
     
  23. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    In the context of uGUI and the beta assets mobile controls how can we get them to avoid conflicts. For example how do we know if the touch has landed on the button or in the game world somewhere. It seems like uGUI has it's own code to detect touches and button clicks ect... which runs at the same time and conflicts with uGUI.
     
  24. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    You can try something like this:

    Code (csharp):
    1.  
    2.     //inputID=-1 - mouse cursor,    inputID>=0 - touch finger index
    3.      public static bool IsCursorOnUI(int inputID=-1){
    4.        EventSystem eventSystem = EventSystem.current;
    5.        return ( eventSystem.IsPointerOverGameObject( inputID ) );
    6.      }
    7.  
    make sense?
     
  25. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    do you think you can explain whats happening in that code and why it needs to happen? thanks.
     
  26. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Not much to explain there. This is basically the API for uGUI to test if any cursor/touch is placed on any UI element.

    http://docs.unity3d.com/460/Documen...tems.EventSystem.IsPointerOverGameObject.html

    it will return true if there's a cursor hover on top of the UI element, false if otherwise. The inputID indicate what cursor you want to test for, -1 for mouse, 0 and above means touch with the matching finger index. Of course all this is not defined by me but rather by the inner working of the uGUI.

    When an event is fired, you can call this function to check if the cursor/finger position is landed on a ui element.
     
  27. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    Hi, I have purchased your package. I like how it's setup however I have issues. I checked not just my code but also your TapDemo c# and found both to exhibit strange problems. The biggest problem is short/long/double/charge events are not fired correctly, and sometimes drags do not work or are stuck dragging with nothing touching the screen. I have tested both demo and my code on two devices a Lenovo Yoga 2 13.3, and a Galaxy S5. I have tinkered with the settings and cannot seem to fix it. I've tried making dpi lower 150 and higher 400 as well as changing the width tolerances to larger values, still seems broken.
     
  28. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Unfortunately I dont have either of the device you stated. The closest I have is a GalaxyS4. I have to say i dont suffer any issue you mentioned at all. Does the demo works on Unity editor when using mouse?
     
  29. fschneider

    fschneider

    Joined:
    May 26, 2014
    Posts:
    42
    Not sure whether that has been asked before: Is there a way to stop other handlers from receiving tap events? My use case is that I am having a modal window with elements in it listening for tap events, and, underneath that window, another element listening for tap events. Both will receive a callback, but I would like to prevent the callback for the lowermost element if the one above (the one in the modal window) was hit.
     
  30. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    Here's the deal, I have gotten it working pretty good on my S5, though it does miss a press every now and then, and I know this because in dev mode it shows me my touch presses and they are there and Input.Touches occasionally doesn't register them.

    The real meat of the problem is my Lenovo Yoga 2, it just simply doesn't work well with it at all, all the previously explained issues remain even in the same project/demo that works with my s5.
     
  31. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    I've looked into it even further, and it appears that Unity editor and stand alone applications do not support touch, which is a serious limitation and not explained anywhere. I did get touch input to work on my Yoga 2 by building for windows 8.1 application style TapDemo and it seems to work well. I think this limitation should be put in your description and in the docs if it isn't already.
     
  32. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Sorry all for the slow respond. I'm currently away from home and thus doesn't have access to reliable internet connection. I'm hereby apologise in advance for any slow respond. Normal service will be resume by this weekend.

    @DoubleLee, noted. I'll see what I can do. Sorry for the trouble.

    @fschneider, perhaps you can use a common listener and implement a control logic there. Depends on the condition, call the function you need. Make sense?
     
  33. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    Hi, I have feature request. Can you set option to allow the Drag begin to start where the touch first landed and not at the point at which the drag was detected, because this makes my drag box off by whatever the minimum drag distance is, which throws everything off.
     
    Last edited: Dec 11, 2014
  34. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Well, the reason there needs to be a minimum drag threshold before a drag can be confirmed is to prevent drag from being fired when the a touch just landed on the screen without moving. Without that, you will be getting a drag event for every touch landed on the screen. I would suggest you to just set the "MinimumDragDistance" on DragDetector to 1 or 2, The offset of 1 or 2 pixel shouldnt be visible or make any difference on most device. You can even set the threshold to 0 if you wish. Beware tho doing that means every touch on the screen triggers a drag event.
     
  35. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    I think we misunderstand each other. Can't you store the original position of the press and return that as the drag start position?
     
  36. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Yes, the start position is not stored. But you can calculate it yourself based on the information given. Just use the position of the drag and deduct drag delta.

    Vector2 startPos=dragInfo.pos-dragInfo.delta;
     
  37. Squi_Beta

    Squi_Beta

    Joined:
    Dec 11, 2013
    Posts:
    5
    Hello,

    I'm trying to make swipe controls for my game. The player moves in a grid and when a raycast (coming out from his center to the 4 directions) hits something, the player can't move in that direction.

    Here is my code, I tried this way to make the player move straight, but it doesn't work very well and I can't figure out how to add the other 3 movement directions.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TouchInputs : MonoBehaviour {
    5.  
    6.     public float rayLenght;
    7.     private RaycastHit hit;
    8.     private Ray straightRay;
    9.  
    10.     void OnEnable(){
    11.         IT_Gesture.onSwipeEndE += OnSwipe;
    12.     }
    13.    
    14.     void OnDisable(){
    15.         IT_Gesture.onSwipeEndE -= OnSwipe;
    16.     }
    17.    
    18.     void Update() {
    19.         straightRay = new Ray(transform.position, Vector3.right);
    20.        
    21.         Debug.DrawRay(transform.position, Vector3.right * rayLenght);
    22.     }
    23.    
    24.     void OnSwipe(SwipeInfo sw){
    25.    
    26.         if(sw.direction.y >= sw.direction.x) {
    27.             Debug.Log("swipe");
    28.             if(Physics.Raycast(straightRay, out hit, rayLenght)) {
    29.                 if(hit.collider.tag == "Wall") {
    30.                     Vector3 position = this.transform.position;
    31.                     position += new Vector3(0,0,0);
    32.                     this.transform.position = position;
    33.                 }
    34.             }
    35.             else {
    36.                 Vector3 position = this.transform.position;
    37.                 position += new Vector3(4,0,0);
    38.                 this.transform.position = position;
    39.             }
    40.         }
    41.     }
    42. }
    The first thing that came to my mind was that if the Y direction of the swipe is bigger than the X, than the player wants to move straight. The problem comes when I have to add the other movements.

    Can you help me?

    Thanks! :D
     
  38. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    I sort of get what you are trying to do. You can try something like this:
    Code (csharp):
    1.  
    2.    public float raycastLength=1;
    3.    private RaycastHit hit;
    4.  
    5.    void OnSwipe(SwipeInfo){
    6.      if(Mathf.Abs(sw.direction.y)>Mathf.Abs(sw.direction.x)){
    7.        int sign=sw.direction.y>0 ? 1 : -1;   //determine the direction
    8.        bool castHit=Physics.Raycast(transform.position, sign*Vector3.right, out hit, raycastLength);
    9.        if(castHit && hit.collider.tag=="wall") return;     //if we hit a wall, do nothing
    10.        transform.position += new Vector3(sign*4,0,0);   //else move along x-axis
    11.      }
    12.      else{
    13.        int sign=sw.direction.z>0 ? 1 : -1;
    14.        bool castHit=Physics.Raycast(transform.position, sign*Vector3.forward, out hit, raycastLength);
    15.        if(castHit && hit.collider.tag=="wall") return;
    16.        transform.position += new Vector3(0,0,sign*4);
    17.      }
    18.    }
    19.  
    I'm not sure when if you are moving along xy plane or xz plane. But the code above assume you are trying to move along xz plane. Hope this make sense.
     
  39. Squi_Beta

    Squi_Beta

    Joined:
    Dec 11, 2013
    Posts:
    5
    Thanks for the tip but I get this error

    at this line

    Code (CSharp):
    1. int sign=sw.direction.z>0 ? 1 : -1;
     
  40. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    My apologies, just change it to int sign=sw.direction.y>0 ? 1 : -1;.
     
  41. Casanuda

    Casanuda

    Joined:
    Dec 29, 2014
    Posts:
    53
    Hi,

    This is driving me crazy and we be grateful if you could help.

    Using the "input touches get dragging info" action and then applying the "vector 2 normalise" action gives me strange results.

    I eventually ran the playmaker get touch info side by side and the discrepancy was obvious.

    eg.
    "Get touch info action" finger top right of device: vector: 1.0,1.0,1.0 as expected
    input touches get dragging info finger top right of device: vector: 1.0,0.3

    But the co-ordinates seemed wrong all over the screen

    Is this a bug?

    Thanks
    Filippo
     
  42. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Right, which vector2 member of DragInfo you are using, pos or delta? Hopefully it's pos cause delta is the drag mafnitude, not the finger position. Also you seems to have misunderstood what normalise does. Normalise simply rescale the vector so that it's magnitude is 1. To start with, the magnitude of vector (1, 1) is not 1, it's actually 1.414. Also typically a device screen resolution ratio is not 1:1. For instance the resolution of iPhone is 960x640 so the ratio is 3:2. At the top corner the cursor position is (960, 640), normalising that is going to give you (0.8, 0.6) which has a magnitude of 1.

    What you need to do to get (1.0, 1.0) at the top corner is something like this:
    Code (csharp):
    1. Vector2 vector=new Vector(dragInfo.pos.x/Screen.width, dragInfo.y/Screen.height);
     
  43. briwil78

    briwil78

    Joined:
    Jan 3, 2015
    Posts:
    4
    Hello, I just bought the asset today, and as I'm trying to run some of the example scenes, I keep getting this error when I try to drag/touch something-
    I also notice (may be related, or not) that in my InputTouches obj, I see a 'Gesture' script, followed by 'Tap Detector' script, followed by a blank script component that says "Missing(MonoScript)" inside it.

    This can't be good- I don't even know what script is supposed to be there, or why it would be missing?

    Also, the 'Gesture' script is giving me a warning that "The associated script can't be loaded. Please fix any compile errors and assign a valid script".

    Running Unity 4.6.1.p2
    Any help would be appreciated, really want to dive into this asset.

    --------
    Edit- After looking at the Package Contents in the asset store, I realized that in the InputTouches folder there is a script called "IT_Gesture.cs", but that same script was simply titled "Gesture.cs" in the package that I had downloaded from the asset store.
    Changing the script name to "IT_Gesture.cs" seemed to fix the script not loading issue- maybe take a look at that and update the package?
    But I still have a missing script showing up.
     
    Last edited: Jan 3, 2015
  44. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Thanks for your purchase.

    Sound like something is wrong with your import process. I would suggest you to try to remove all relevant file, re-download and re-import again. Or to be completely error-proof, import it to an empty project. I've just tried the process myself. There's no Gesture.cs and no missing script component. In fact, this is how the InputTouches prefab looks like:

     
  45. arnaud74130

    arnaud74130

    Joined:
    Jan 6, 2015
    Posts:
    1
    Hi,
    I have imported version 1.2f1 from the assetStore and I can't find the IT_Gesture.onTouchPosDownE definition ??
     
  46. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    It's in IT_Gesture.cs. It should be onTouchDownPosE instead of onTouchPosDownE. I realise there's some error in the documentation. My apologies...
     
  47. demock

    demock

    Joined:
    Nov 16, 2013
    Posts:
    2
    Thank you for the great add-on.

    I'm using Unity Pro 4.5.4f1 and having following issue.


    Will there be any possible explanation?

    Regard.
     

    Attached Files:

  48. Song_Tan

    Song_Tan

    Joined:
    Apr 9, 2011
    Posts:
    2,993
    Did you modify the script in any way? I manage to replicate the exact same errors when I change

    void OnMultiTap(Tap tap) to void OnMultiTap()

    and

    void OnLongTap(Tap tap) to void OnLongTap

    in TapDemo.cs. Please check your script to see if that's the case.
     
    demock likes this.
  49. demock

    demock

    Joined:
    Nov 16, 2013
    Posts:
    2
    My bad, It was just a mere collision with Easy Touch asset.

    Thank you for your help.
     
  50. 2Dboomer

    2Dboomer

    Joined:
    Jul 16, 2012
    Posts:
    35
    Hey Songtan!

    I used the Input.Touches for two years, and it works very well.

    I want to ask is it support IL2CPP for 64bit now?

    Thanks!