Search Unity

Move to platform only if it exists

Discussion in 'Scripting' started by C4lcab, Sep 27, 2016.

  1. C4lcab

    C4lcab

    Joined:
    Sep 27, 2016
    Posts:
    6
    Hey guys, I'm new here and I'm working on a 2D puzzle.

    To explain what I want, basically I'll have a character and it will move to an adjacent block according to the user input.



    I'll try to explain use this image.

    The blue alien is my character and I want it to move to the right block when my player swipes right, to the left block when my player swipes left and etc, but only if there's a block there. The problem I'm having is that I don't have any idea of how to detect if there's an adjacent block in the direction of the swipe.

    Basically I want to:

    Player swipes > Check if there's a block in the direction of that swipe > If yes, move to that block.

    I already have the swipe script working, by the way.

    Thanks in advance!
     
  2. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    Your image didn't work but the best solution would be to set up a 2 dimensional array that is going to mirror your game board. Give each square a coordinate based on its position in the group, with the bottom left corner being 0, 0. Each time an object moves around in the game world, move it in the array as well. Now you can look up each object by simply checking the array and seeing if there is an object there.