Search Unity

Drag and Drop with click to select

Discussion in '2D' started by jc1231, Jan 22, 2020.

  1. jc1231

    jc1231

    Joined:
    Nov 8, 2019
    Posts:
    8
    I have a working solitaire game and I'd like to enhance the input.

    The two things I'd like to accomplish together are:
    - Drag and drop a card to a valid play spot. If you drop the card in an invalid place, it's postilion will revert to it's original location.
    - Single click to select a card, then click to another spot or card to move it.

    I can get both of these working just fine individually but can't figure out how to make them both work at the same time. IIRC this is how the old windows Solitaire controls worked.

    I'm using `Input.GetMouseButtonDown` in the Update method and RayCasting for collision detection.
     
  2. oharinth

    oharinth

    Joined:
    Jul 8, 2015
    Posts:
    25
    I was similar situation.
    Use drag and drag event class for mouse drag. And use mouse click event class for mouse click. Both of them needed.

    For example, make class start to following ( for mouse drag and drop) :

    public class ItemDrag : MonoBehaviour,
    IBeginDragHandler,
    IDragHandler,
    IEndDragHandler

    For example, make class start to following ( for mouse click):

    public class ItemClick : MonoBehaviour,
    IPointerUpHandler,
    IPointerDownHandler,
    IPointerClickHandler

    -----
    My example is just part of them.
    But if you find the both of class reference you could make your own codes.