gorokizu
gorokizu

Reputation: 2588

Dragging composite objects in Unity

I am writing a card game in Unity2D that requires dragging a card to a table that has fixed positions. The card is a composite gameobject that contains these items:

I want to be able to drag the card to the table and remove it from the player's hand. While i have found some implementations of drag/drop they all seem to rely on dragging a single image and not a gameobject. What can i use to accomplish this? Thanks in advance

Upvotes: 1

Views: 171

Answers (1)

Iggy
Iggy

Reputation: 4888

Implement IBeginDragHandler, IDragHandler, IEndDragHandler interfaces in your script that is attached to the draggable game object.

public void OnBeginDrag(PointerEventData eventData) {
    // Set parent to a RectTransform that is in front of everything else 
    this.transform.SetParent(draggablesRoot);
}

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

public void OnEndDrag(PointerEventData eventData) {
    // Use "EventSystem.current.RaycastAll()" to detect whether the object was dropped onto the correct panel
}

Upvotes: 1

Related Questions