1
\$\begingroup\$

Figured out that this code only works on 3D colliders so how do I change it so that I can use it on 2D colliders?

Below is the c# code.

using UnityEngine;

// This script allows you to drag this GameObject using any finger, as long it has a collider public class SimpleDrag : MonoBehaviour { // This stores the layers we want the raycast to hit (make sure this GameObject's layer is included!) public LayerMask LayerMask = UnityEngine.Physics.DefaultRaycastLayers;

// This stores the finger that's currently dragging this GameObject
private Lean.LeanFinger draggingFinger;

protected virtual void OnEnable()
{
    // Hook into the OnFingerDown event
    Lean.LeanTouch.OnFingerDown += OnFingerDown;

    // Hook into the OnFingerUp event
    Lean.LeanTouch.OnFingerUp += OnFingerUp;
}

protected virtual void OnDisable()
{
    // Unhook the OnFingerDown event
    Lean.LeanTouch.OnFingerDown -= OnFingerDown;

    // Unhook the OnFingerUp event
    Lean.LeanTouch.OnFingerUp -= OnFingerUp;
}

protected virtual void LateUpdate()
{
    // If there is an active finger, move this GameObject based on it
    if (draggingFinger != null)
    {
        Lean.LeanTouch.MoveObject(transform, draggingFinger.DeltaScreenPosition);
    }
}

public void OnFingerDown(Lean.LeanFinger finger)
{
    // Raycast information
    var ray = finger.GetRay();
    var hit = default(RaycastHit);

    // Was this finger pressed down on a collider?
    if (Physics.Raycast(ray, out hit, float.PositiveInfinity, LayerMask) == true)
    {
        // Was that collider this one?
        if (hit.collider.gameObject == gameObject)
        {
            // Set the current finger to this one
            draggingFinger = finger;
        }
    }
}

public void OnFingerUp(Lean.LeanFinger finger)
{
    // Was the current finger lifted from the screen?
    if (finger == draggingFinger)
    {
        // Unset the current finger
        draggingFinger = null;
    }
}

}

\$\endgroup\$
1
\$\begingroup\$

The Physics class is for 3D (as you guessed) so what you need to do is change your code to use the 2D Physics. Aka Physics2D, this will collide with 2D colliers. So for the OnFingerDown method you'll need to change the physics call:

public void OnFingerDown(Lean.LeanFinger finger)
{
    // Raycast information
    var ray = finger.GetRay();
    var hit = default(RaycastHit2D);

    // Was this finger pressed down on a collider?
    if (Physics2D.Raycast(ray, out hit, float.PositiveInfinity, LayerMask))
    {
        // Was that collider this one?
        if (hit.collider.gameObject == gameObject)
        {
            // Set the current finger to this one
            draggingFinger = finger;
        }
    }
}

That should work with 2D colliders:)

Side note, when you have somthing that returns a bool and your using things that evaluate true or false to work you don't need to have them work it out again. For the raycast it returns true/false already so there is no need to check if it's true. The more you know....

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.