Search Unity

Lean Touch by Carlos Wilkes

Discussion in 'Assets and Asset Store' started by jshrek, Mar 11, 2015.

  1. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    I found this free asset in the store and it is pretty awesome! Anybody else using it? Let's you use mouse in Unity editor to simulate touch (and then let's you actually use touch on real mobile device).

    The only issue I have come across, is using SimpleDrag example, if two different objects colliders are overlapping, it seems to pick the one on top. Is there a way to ignore one collider all the time because I am not interested in that one at all.

    Here is Lean Touch relevant script:
    Code (csharp):
    1.  
    2.     // Raycast information
    3.      var ray = finger.GetRay();
    4.      var hit = default(RaycastHit);
    5.      
    6.      // Was this finger pressed down on a collider?
    7.      if (Physics.Raycast(ray, out hit) == true) {
    8.        // Was that collider this one?
    9.        if (hit.collider.gameObject == gameObject) {
    10.          // Set the current finger to this one
    11.          draggingFinger = finger;
    12.        }
    13.      }
     
    DevinDevelopson likes this.
  2. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    Okay so to answer my own question, I found that you can assign an object to a layer called IgnoreRaycast which works in my case to ignore that object.

    But would still be good to know if there is a way to exclude a particular object when doing
    Code (csharp):
    1. if (Physics.Raycast(ray, out hit) == true)
     
  3. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    Evaluating Lean Touch now. I was looking for solution what would save me having to write # If directives everywhere when testing in the editor and developing for touch screens. I'm liking it. Much moreso than TouchKit .. Didn't have to spend half a day figuring it out.

    Maybe oneday I'll revisit the Prime[31] solution, but as of now, I found it too convoluted for my rapid iterative purposes.

    About overlapping colliders...
    Well, that just plain makes sense. You can't pick something up if there is something on top of it. Think a stack of books... Cant move the book on the bottom without moving the books off the top.

    Use tags

    Code (CSharp):
    1. if (Physics.Raycast(ray, out hit, 100))
    2.          {
    3.             if (hit.collider.tag == _tag)
    4.             {
    5.               // Do something fancy
    6.             }
    7.          }
     
  4. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    Carlos (the author of Lean Touch) has suggested the following modification to his SimpleDrag.cs script which allows you to choose which layers you want to be included/excluded. So you can assign only the objects you want to an included layer and it will ignore all the objects in the other layers (but those objects can now still be used by Raycast in other scripts if needed).

    He added near the top:
    Code (csharp):
    1. public LayerMask LayerMask = UnityEngine.Physics.DefaultRaycastLayers;
    And then changed the IF statement to:
    Code (csharp):
    1. if (Physics.Raycast(ray, out hit, float.PositiveInfinity, LayerMask) == true)
    Here is complete updated SimpleDrag.cs script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. // This script allows you to drag this GameObject using any finger, as long it has a collider
    4. public class SimpleDrag : MonoBehaviour
    5. {
    6.  
    7.     // This stores the layers we want the raycast to hit (make sure this GameObject's layer is included!)
    8.     public LayerMask LayerMask = UnityEngine.Physics.DefaultRaycastLayers; //ADDED LAYER SELECTION
    9.  
    10.     // This stores the finger that's currently dragging this GameObject
    11.     private Lean.LeanFinger draggingFinger;
    12.  
    13.     protected virtual void OnEnable()
    14.     {
    15.    
    16.         // Hook into the OnFingerDown event
    17.         Lean.LeanTouch.OnFingerDown += OnFingerDown;
    18.    
    19.         // Hook into the OnFingerUp event
    20.         Lean.LeanTouch.OnFingerUp += OnFingerUp;
    21.     }
    22.  
    23.     protected virtual void OnDisable()
    24.     {
    25.  
    26.         // Unhook the OnFingerDown event
    27.         Lean.LeanTouch.OnFingerDown -= OnFingerDown;
    28.    
    29.         // Unhook the OnFingerUp event
    30.         Lean.LeanTouch.OnFingerUp -= OnFingerUp;
    31.     }
    32.  
    33.     protected virtual void LateUpdate()
    34.     {
    35.    
    36.         // If there is an active finger, move this GameObject based on it
    37.         if (draggingFinger != null)
    38.         {
    39.             Lean.LeanTouch.MoveObject(transform, draggingFinger.DeltaScreenPosition);
    40.         }
    41.     }
    42.  
    43.     public void OnFingerDown(Lean.LeanFinger finger)
    44.     {
    45.    
    46.         // Raycast information
    47.         var ray = finger.GetRay();
    48.         var hit = default(RaycastHit);
    49.    
    50.         // Was this finger pressed down on a collider?
    51.         if (Physics.Raycast(ray, out hit, float.PositiveInfinity, LayerMask) == true) { //ADDED LAYER SELECTION
    52.         //if (Physics.Raycast(ray, out hit) == true) { //OLD METHOD with no layer selection, which will only filter out IgnoreRaycast layer
    53.  
    54.             // Was that collider this one?
    55.             if (hit.collider.gameObject == gameObject)
    56.             {
    57.                 // Set the current finger to this one
    58.                 draggingFinger = finger;
    59.             }
    60.         }
    61.     }
    62.  
    63.     public void OnFingerUp(Lean.LeanFinger finger)
    64.     {
    65.         // Was the current finger lifted from the screen?
    66.         if (finger == draggingFinger)
    67.         {
    68.             // Unset the current finger
    69.             draggingFinger = null;
    70.         }
    71.     }
    72. }
     
    Last edited: Mar 12, 2015
    FuguFirecracker likes this.
  5. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    Sorry, my reading comprehension was a little off... Of course Tags won't work if you need to click through an object.

    Thanks for the updated code.
     
  6. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    The other minor issue I ran into was when I paused my game (using Time.timeScale = 0; ) was that you could still drag the player object around the screen.

    So I changed line 37, from this:
    Code (CSharp):
    1. if (draggingFinger != null)
    To this:
    Code (CSharp):
    1. if (draggingFinger != null && Time.timeScale != 0)
     
    Last edited: Aug 18, 2015
  7. zederis

    zederis

    Joined:
    Mar 29, 2015
    Posts:
    1
    Hello guys,
    Looks like it is a really great asset, but can someone tell me how to set up it. What parts of code should i modify? I am new to scripting.. Thank you :)
     
  8. ransomink

    ransomink

    Joined:
    Aug 18, 2013
    Posts:
    3
    Hey guys, I was wondering if anyone was able to get LeanTouch working with Unityscript. I've tried but am still having trouble...
     
  9. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    Yes, works great for me.

    1 - You put the Lean Touch folder into your Projects section.

    2 - In your hierarchy, you create a new empty object and rename it LeanTouch.

    3 - Within that same object, Add Component and attach the Lean > Touch script.

    4 - Now in your hierarchy click on the "object you want to drag" and click Add Component and attach the Scripts > Simple Drag script.

    Now test and drag away!
     
    jzairus likes this.
  10. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. // This script will move the GameObject based on finger gestures
    4. public class SimpleMove : MonoBehaviour
    5. {
    6.     protected virtual void LateUpdate()
    7.     {
    8.         // This will move the current transform based on a finger drag gesture
    9.         Lean.LeanTouch.MoveObject(transform, Lean.LeanTouch.DragDelta);
    10.     }
    11. }
    How to disable dragging to Y position. Drag only X position?
     
    Alex_Designer likes this.
  11. Spartan_Boy

    Spartan_Boy

    Joined:
    Jul 8, 2012
    Posts:
    38
    Hi all, great free asset! I played with it a bit and made a few exercices with it for my students and they enjoyed it alot.
    Has anyone managed to limit the move/rotate/scale to one object, because at the time it seems to affect every gameobject within the range of the touch?
    Great asset i strongly recommend it for people starting doing touch in Unity.
     
  12. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    @Spartan_Boy Go back to the top of this thread and read the whole thing as I have posted a couple options for including/excluding objects.
     
  13. Spartan_Boy

    Spartan_Boy

    Joined:
    Jul 8, 2012
    Posts:
    38
    @jshrek, ok i added the layer mask function which seems to do the trick, thx, i thought it had to be handled per object, not by layer. Did you give the double-tap a go by any chance ? THX for your answer.
     
    imandic likes this.
  14. liiwei

    liiwei

    Joined:
    Mar 30, 2016
    Posts:
    1
    Anyone seems to know how to make this code to work with 2d sprites?
     
  15. kdarius43

    kdarius43

    Joined:
    Mar 16, 2015
    Posts:
    170
    Hello I too am working with this great plug in. A question I had is for the simple move... is there anyway to make it so instead of moving and coming to an abrupt stop the gameobject could slide past the stop point and ease to a stop.
     
  16. Killerdawn

    Killerdawn

    Joined:
    Sep 6, 2016
    Posts:
    1
    hello have been import lean touch to my assets and the simple move and simple rotate is not been added or any available at the package i really want that script is anyone here where can i find it??
     
  17. Hjortebjerg

    Hjortebjerg

    Joined:
    Apr 16, 2016
    Posts:
    1
    HI. Is anyone familiar with a combination of Vuforia AR SDK and the Lean touch scripts. I am creating a project in which I have multiple child objects of my image target. (a cube, a sphere etc.) When I try to add the transform functionality from the lean touch script it transforms all objects even though I only pick (and touch) one of them. I can't seem to get the solution above to work... Maybe it is me that doesn't understand the layer mask. Is there an alternative solution??? Please help.
     
    Noor23 likes this.
  18. mqelbajrie

    mqelbajrie

    Joined:
    Nov 24, 2016
    Posts:
    1
    Hi, sory how to script output info scale in lean touch. thanks
     
  19. pl_ayground

    pl_ayground

    Joined:
    Dec 17, 2015
    Posts:
    60
    I am having issues using this with Windows Phone. Is this a known issue? Works like charm on Android and iOS but pinch-zoom does nothing on Windows Phone.

    Lean.Touch.LeanTouch.PinchScale seems to be always 1 ??
     
  20. firativerson

    firativerson

    Joined:
    Jan 22, 2015
    Posts:
    33
    Did you find a workaround for this? Scaling rate is too high I am trying to slow it down but that 1 value is crucial. Didnt really look into it yet but if there is any quick fix i am willing to take it :D
     
  21. ptachaneu

    ptachaneu

    Joined:
    Feb 24, 2017
    Posts:
    7
    I get multiple errrors using the Simple Drag script. Do I have to edit it somehow to make it valid?

    Thanks!
     
  22. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Hi. I need to pass a custom object using the LeanTouchEvent.

    ie the LeanTouchEvent OnFIngerDown handler or delegate would then be
    Code (csharp):
    1.  
    2. public void OnFingerDown(LeanFinger finger,string ob)
    3.  
    When I do this I get the error
    A method or delegate `Lean.Touch.LeanTouchEvents.OnFingerDown(Lean.Touch.LeanFinger, string)' parameters do not match delegate `System.Action<Lean.Touch.LeanFinger>(Lean.Touch.LeanFinger)' parameters

    Should I be using SendMessage(string) instead?
    I've read that SendMessage is very inefficient.
     
  23. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Hi
    One other point of confusion.


    Right now I have 3 scenes that get loaded into my Main Scene.
    Each of these 'child' Scenes has an instance of both LeanTouch and LeanPressSelect.
    Is there any problem with this or should I have only 1 instance of LeanTouch and LeanPressSelect in my root or Main Scene?
     
  24. wilbertllama

    wilbertllama

    Joined:
    Mar 6, 2017
    Posts:
    1
    Hello,
    I'm following this tutorial:


    Question 1: In my project, I have 2 image targets and two 3D models attached to them. If I apply Lean Touch (as in the video) in both 3D models, I can move, scale, and rotate the two 3D models. However, If I move/scale/rotate one 3D model, then the other 3D model does the same thing. I really would like to do that seperately for the two 3D models. Do you know what I can do?

    Question 2: If my android phone recognizes an image target and augment a 3D model, I can rotate/scale/move it freely. If I remove the image target away from the phone’s view and then put in the view of the phone again, then the same transformations (for example when I make my 3D model very small with scale the first time) still apply the second time I show the image target to the phone. How can I reset the transformations to its default settings (The very begin position of the Augmented 3D Model) when my phone detect the image target for the second time?
     
  25. xiHoodyix

    xiHoodyix

    Joined:
    Jul 3, 2017
    Posts:
    1
    I am using this asset for the exact same use as you wilbert, and it would be very helpful to know how to do this!
     
  26. yaffa

    yaffa

    Joined:
    Nov 21, 2016
    Posts:
    41
    How do I access the current selectedObject using Leantouch?
    I can see that if I add leanTapselected script , it shows the last tapped object , how do I access this via scrpt?
     
  27. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Hi Carlos
    I need to distinguish between a 1 and 2 finger select(or press) action. Is this possible with LeanTouch? If so, which example should I be referencing. Thanks!
     
  28. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    One further question. I need BOTH simple swipe detection AND press-select detection. However, if I enable both LeanPressSelect AND SwipeDirection 4 classes in the same scene I get a runtime error
    pointing to FingerUp method in leanPressSelect class

    Code (csharp):
    1.  
    2. private void FingerUp(LeanFinger finger)
    3.         {
    4.        
    5.   var listObject = CurrentSelectables[0];//error thrown here
    6.  
     
  29. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Help!!!!.......
    Using LeanSelectable component on a series of Quads. It IS working in one scene. Can receive BOTH mouse and Touch events.
    However, I cannot get it working in another scene. If I simple copy, paste a single Quad into a new scene, compile and run in the Editor, making sure scripts are attached, it doesn't work!
    Pulling my hair out. I must be missing something obvious.
    I've created a bare bones scene illustrating the problem. You just need to create a new project and import the attached package.
    Any feedback welcome as I am up against a deadline this weekend
    Thanks in advance!
     

    Attached Files:

  30. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Hi Carlos
    Developing a mobile app. After unsuccessfully trying to modify LeanTranslate Smooth to add smoothing and inertia to a simply select drag, I want to ONLY select and drag without inertia or smoothing.

    Could you possibly supply a bare bones example?
     
  31. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Can anyone suggest how I would override the Select method in LeanSelectable so that it works with OnPointerDown or OnPointerClick?

    I am trying to make the Select method ignore any Unity UI touch or mouse events.
    Here is my modified Select method
    It seems to be ignoring if(!finger.IsOverGui
    Code (csharp):
    1.  
    2. override public void Select(LeanFinger finger)
    3.     {
    4.         if (isSelected == false)
    5.         {          
    6.             if (!finger.IsOverGui && _selectable == true)
    7.             {
    8.                 base.Select(finger);          
    9.             }
    10.         }
    11.     }
    12.  

    ie the 3rd method in this video tutorial on blocking UI clicks
     
    Last edited: Jan 1, 2018
  32. abricamt

    abricamt

    Joined:
    Jan 13, 2018
    Posts:
    2
    Help i got this error

    Assets/scripts/leantouch.cs(69,31): error CS0234: The type or namespace name `LeanFinger' does not exist in the namespace `Lean'. Are you missing an assembly reference?
     
    Ichihara13 likes this.
  33. abricamt

    abricamt

    Joined:
    Jan 13, 2018
    Posts:
    2

    I got this error while using this code of yours, what does it means? Thanks

    Assets/scripts/leantouch.cs(69,31): error CS0234: The type or namespace name `LeanFinger' does not exist in the namespace `Lean'. Are you missing an assembly reference?
     
    Ichihara13 likes this.
  34. diegoadrada

    diegoadrada

    Joined:
    Nov 27, 2014
    Posts:
    59
    I need to work with OnFingerDown and OnFingerSwipe at the same time, if the user touch the screen the character has to jump, and if the user makes a swipe in any direction, the attack mechanic will be activate. But right now the OnFingerDown event is always been activate even if the user tries to make a swipe, has someone had to work with something similar using this asset? Thanks!
     
  35. novaVision

    novaVision

    Joined:
    Nov 9, 2014
    Posts:
    518
    I found that OnFingerUp event triggers when I move out mouse outside the screen in Editor mode, and and OnFingerDown triggers just after I return mouse to the screen area. How to avoid it, because it's a bit annoying while testing fucntionality
     
  36. ikaraimi4

    ikaraimi4

    Joined:
    Apr 5, 2018
    Posts:
    1
    Hi i having a problem when running the code , it said that the lean touch script do not contain the "Move Object" definition. Can u help me? Capture.PNG
     
    Ichihara13 and BJosephine like this.
  37. inbal

    inbal

    Joined:
    Jul 6, 2017
    Posts:
    10
    Problem!
    I use LeanTranslate to move parts around - Is There any way to Reset the Scene to Default State?
     
    BJosephine likes this.
  38. unity_jt52A6kzJLCuuA

    unity_jt52A6kzJLCuuA

    Joined:
    Aug 1, 2018
    Posts:
    2
    Does anyone know how to add listeners to the events via script? I keep getting errors for...
    Code (CSharp):
    1. setSelectable.OnSelect.AddListener(() => { setSelectable.RevisionTrackingEnableMovement(); });
     
  39. aurelliavee

    aurelliavee

    Joined:
    Jul 20, 2018
    Posts:
    1
    Hi, does anyone know how to rotate with 1 finger only using LeanTouch?
     
  40. Jabu33

    Jabu33

    Joined:
    Sep 3, 2018
    Posts:
    4
    Hi,
    I am trying to work with LeanTouch and latest ARcore but the LeanTouch added to the scene does not receive any screen touch from debugged device when ARdevice is present, but 'mouse' clicks on Game device are working fine.

    I have just basic HelloAR with added LeanTouch. When I am debugging the app, in the ExampleController script I have debug loggers:

    Debug.Log("LeanCount " + LeanTouch.Fingers.Count);

    Printing finger count in LeanTouch - working only on Game preview mouse clicking in Unity.
    Debug.Log("InputCount " + Input.touchCount);

    Second printing finger count in Input.touchCount finger tapping on device.

    Any advice how to use Lean inside the AR ExampleController I want to scale/rotate/move objects spawned in AR on detected Ground Plane.
     
  41. StefanoCecere

    StefanoCecere

    Joined:
    Jun 10, 2011
    Posts:
    211
    i have the same ptoblem: mouse clicks work in editor, real touches don't work in device
     
    parrapazd likes this.
  42. Volkerku

    Volkerku

    Joined:
    Nov 23, 2016
    Posts:
    113
    How would I combine Lean Translate (I'm using MultiUpdate with ManualTRanslate) and FingerSwipe?
    I would like to avoid that an object moves when a swipe is detected. The swipe should control the object ID not the position. I tried saving teh position at touch.begin and setting it back to this value if a swipe was detected (delta threshold). But it keeps moving, I think top do with the MultiUpdate.
     
  43. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    30
    I love this asset.
    But how do you stop it from detecting multiple fingers in the swipe settings, I only need one, how to deactivate multifinger/touch swipe feature?
     
  44. ThisLove

    ThisLove

    Joined:
    Jun 27, 2019
    Posts:
    5
    Hello,

    I'm sorta at a loss, I'm pretty new to events and this code gives me the error that the OnSelectedFinger is read only

    public LeanSelectableByFinger lsbf;
    // Start is called before the first frame update
    void Start()
    {
    lsbf.OnSelectedFinger += FingerTap;
    }

    void FingerTap(LeanFinger finger)
    {
    //make a white cicle appear and have the rose circle have a minimum distce of just biggerthan the white
    selectingFinger = finger;
    fingerStartingPosition = selectingFinger.ScreenPosition;
    }
     
  45. parrapazd

    parrapazd

    Joined:
    Jun 28, 2021
    Posts:
    2
    [QUOTE = "StefanoCecere, publicación: 3843079, miembro: 49184"] tengo el mismo problema: los clics del mouse funcionan en el editor, los toques reales no funcionan en el dispositivo [/ QUOTE]
    Hola, lograste resolver esto? Me seria de mucha ayuda :(
     
  46. parrapazd

    parrapazd

    Joined:
    Jun 28, 2021
    Posts:
    2
    Hola, espero alguien vea esto lo antes posibles :(
    Tengo un problema con esto, los clics me funcionan en el editor, sin embargo al pasarlo en apk y probarlo en el dispositivo no me lee los touch.
     
  47. qwertypchimmy12

    qwertypchimmy12

    Joined:
    Jan 18, 2021
    Posts:
    23
    hi there's anyone here? please help me. I've been download and import lean touch asset. But sadly, I don't get the full script. I means I don't have the lean scale, lean rotate, lean move script.
     

    Attached Files:

  48. Noor23

    Noor23

    Joined:
    Feb 19, 2022
    Posts:
    28
    Hi , I have the same issue and can’t figure it out , could you find any solution though ? if yes please help :/
     
  49. Noor23

    Noor23

    Joined:
    Feb 19, 2022
    Posts:
    28
    hello , I know it is an old post but you might see it anyways as I am having an issue that when i drag one item all the rest moves together , I have even imported the old lean touch that has the simple drag script but it did nothing , please do help me out if you could , thanks
     
  50. unity_77609A299314CFF882B1

    unity_77609A299314CFF882B1

    Joined:
    May 21, 2022
    Posts:
    2
    hi, Did you find a solution to it i am facing same issue?