Search Unity

Question Swipecontrols on multiple objects

Discussion in '2D' started by jonph964, Dec 27, 2020.

  1. jonph964

    jonph964

    Joined:
    Jul 26, 2019
    Posts:
    3
    Hello,
    I hope you all had a nice Christmas. I had a little problem regarding touchcontrols. I'm working on a small smartphone game since a few days, but I can't find an optimal solution for my controls.
    In the scene are 9 2D blocks which you should be able to drag all on each other. It should be very snappy and work as explained in the picture using swipecontrols.
    Does anyone know how to code something like this.
    With kind regards,
    -JONAS Philippe

    movement.png
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi.

    This question is not related to 2D, IMO this is a scripting question.

    Actually you have several things to consider. Detecting touches is one thing and how to store a grid data that describes your game board and pieces is another. I would try to figure those out separately and then finally combine all of these.

    1. If touch happened see if it is a swipe, you can use Input.Touch class and TouchPhase (for example). There are several existing tutorials on this topic, I would simply google those. You can also find several articles on how to detect swipes in 4 main directions.

    2. You'll also have to have a way to store and read occupied game board positions. I wouldn't use any Unity features like colliders. Simply use 2D array or something similar. Many puzzle game tutorials can be helpful for this (tetris, match 3, ...).

    3. When you have swiped over a piece, see if another piece in detected swipe direction exists, if not move the piece. Also move the piece until this condition is valid, until the edge of the game board is reached.