Cancel a drag and drop movement


I want to cancel a drag and drop movement programmatically. Is there a way to do this?

I am using right now the interfaces IBeginDragHandler, IDragHandler, IEndDragHandler.

What I am trying to do: I want to cancel the dragging when I leave a certain area. So when I leave the OnEndDrag Method should be called and the drag is ended.

PS: Right now I have an internal variable to see if am still dragging and stop it if I leave the area. Right now, OnEndDrag is called twice. not really ideal.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;

public class DraggableCard : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
	public Text nameText;
	Vector3 startPosition;
	Transform startParent;
	bool dragging = false;

	public EventTrigger parentBox;

	void Start () {
		EventTrigger.Entry entry = new EventTrigger.Entry();
		entry.eventID = EventTriggerType.PointerExit;
		entry.callback = new EventTrigger.TriggerEvent();
		UnityEngine.Events.UnityAction<BaseEventData> callback =
			new UnityEngine.Events.UnityAction<BaseEventData>(PointerExit);
	public string Info {
		get { return nameText.text; }
		set { nameText.text = value; }

	public void OnBeginDrag (PointerEventData eventData)
		dragging = true;
		startPosition = transform.position;
		startParent = transform.parent;

	public void OnDrag (PointerEventData eventData)

		if (dragging) {
			Vector3 pos = transform.position;
			pos.x = eventData.position.x;
			transform.position = pos;

	public void OnEndDrag (PointerEventData eventData)
		if (dragging) {
			StartCoroutine(SmoothMove (transform.position, startPosition,

dragging = false;

	public void PointerExit(BaseEventData data) {
		Debug.Log ("We Left!");
		OnEndDrag (null);


This question is quite old but I did find the answer.

Only allow on drag to work if a flag is set

Make the flag false if you exit the zone so the drop function stops working

Make an OnPointerUp function which sets the flag again, so that you can drag again

Reset the position of the object if OnDrag is called but the flag is false