Search Unity

Grid-Based Movement by dragging

Discussion in 'Scripting' started by Bumblebee77, Sep 18, 2014.

  1. Bumblebee77

    Bumblebee77

    Joined:
    Sep 18, 2014
    Posts:
    7
    Hi,
    Firstly ,sorry for my poor English.

    I need to a movement script (C# or only algorithm idea) for Grid -based movement by dragging with touch.

    For example;

    Player is a quad 2x2 scale. Grid is 2x 2 too,
    When dragging it, it wil only move up,down ,right or left only 1 unit .(to drag direction) and also clone a new static quad (without move script) at the start position.
    if you drag it to somewhere else like forbidden grid, it wil return back to start position.(i can use tweens for this i think )

    I really stuck in this. and need your help.
    Tkanks
     
  2. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You could round the values. For example:

    Code (csharp):
    1.  
    2. Vector3 snappedPosition = transform.position;
    3. snappedPosition.x = Mathf.Round(snappedPosition.x);
    4. snappedPosition.y = Mathf.Round(snappedPosition.y);
    5. snappedPosition.z = Mathf.Round(snappedPosition.z);
    6.  
    7. transform.position = snappedPosition;
    8.  
    If you want to increase the number of units to snap to, just multiply the rounded values with whatever you like.
     
  3. Bumblebee77

    Bumblebee77

    Joined:
    Sep 18, 2014
    Posts:
    7
    thanks