Hello everyone ![]()
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! ![]()