Triggering function when player drops UI on one specific spot?

Hello everyone :slight_smile:

I am having problems figuring out how to make my “drag and drop” system for UI elements work.
I have a card-based system as in Clash-Royale, where you drag cards from the lower part of the screen and put them where you want them do drop.

When I drop a card with my current script, it simply returns back to its original position.
So I want the player to be able to return the card to his hand if he puts it back in its place, but if he drops it above the play-area X function starts.

How can I do this?

Here is my current script for dragging and dropping UI elements:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {

    public static GameObject itemBeingDragged;
    Vector3 startPosition;
    public bool HoldingCard1;
    public bool HoldingTurretCard;
    public GameObject enemyLaneSpawn;


    #region IBeginDragHandler implementation
    public void OnBeginDrag (PointerEventData eventData)
    {
        itemBeingDragged = gameObject;
        startPosition = transform.position;
    }
    #endregion

    #region IDragHandler implementation

    public void OnDrag (PointerEventData eventData)
    {
        transform.position = Input.mousePosition;
    }

    #endregion

    #region IEndDragHandler implementation

    public void OnEndDrag (PointerEventData eventData)
    {
        itemBeingDragged = null;
        transform.position = startPosition;
    }

    #endregion


    public void CheckCard()
    {
        if (itemBeingDragged.tag == ("EnemyCard1"))
        {
            HoldingCard1 = true;
            Debug.Log ("Holding enemy card 1");
        } 
        else {
            HoldingCard1 = false;
        }

        if (itemBeingDragged.tag != ("EnemyCard1"))
        {
            HoldingTurretCard = true;
            Debug.Log ("Holding turret card");
        } 
        else {
            HoldingTurretCard = false;
        }
    }

    void Start () {

        Debug.Log ("DragHandler initialized");
        gameObject.GetComponent<CanvasGroup> ();
    }

    void Update () 
    {
        CheckCard ();
    }
}

Any help would be appreciated! :slight_smile:

Well you just need a function to tell when the item being dragged is over a drop area. You could do this, for example, by having a BoxCollider on the drag item, and a BoxCollider on the drop area. You can check for overlaps manually (quite easy with axis-aligned boxes) in OnDrag, or use the collision methods to detect when a collision begins/ends.

When you detect that the drag is colliding with a drop area, you can raise an event to give some visual indicator (highlighting the drop area for example), and set a flag in your script so you know it’s good. When they stop colliding, unhighlight and clear the flag.

Then in OnEndDrag, just check that flag — if it’s good, do whatever you want to do for a valid drop; otherwise, move it back to the start.

Sounds easy, but can I have box colliders on UI objects? Say I have a big box collider on the middle of the screen. Will i then be able to interact with other objects on the map, like with onMouseEnter?

I’ve never tried it on UI objects, but then I don’t move UI objects around by their transform.position either. :wink: Anything with a transform (even if it’s a RectTransform) can have a box collider, so if your dragging code works, the bounds checks I outlined should work too.

(I have less confidence in things like onMouseEnter working… experiment and see.)

Works like a charm! Thanks a lot :smiley:

1 Like