Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a fast way to detect drag events on a game object in Unity?

Basically, I am designing a game where I need a canvas image element to respond quickly and move along when it is dragged. So for that, I am implementing IDragHandler interface. But the method OnDrag gets called after a delay of about half second.

public class InputHandler : MonoBehaviour, IDragHandler
{
    public void OnDrag(PointerEventData eventData)
    {
        // this gets logged after about 500 milliseconds
        Debug.Log("mouse dragged");
    }
}

I've attached this script on the same image element that I want to move along with the drag. This element has raycast target on and the graphic raycast component is enabled for its parent canvas. The image element falls behind the touch point initially but catch up later on because of which it feels like there is some lag in the input. Is there a way to remove/minimize this delay?

like image 602
Maxkiel Avatar asked Oct 19 '25 01:10

Maxkiel


1 Answers

The lag might be due to the DragThreshold that is active by default. You can disable it this way:

public class OnDragScript : MonoBehaviour, IDragHandler, IInitializePotentialDragHandler
{
    public void OnDrag(PointerEventData ped)
    {
        transform.Translate(ped.delta);
    }

    public void OnInitializePotentialDrag(PointerEventData ped)
    {
        ped.useDragThreshold = false;
    }
}
like image 89
kefren Avatar answered Oct 21 '25 22:10

kefren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!