Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Click Drag

Discussion in 'Scripting' started by Milambur88, Feb 12, 2020.

  1. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    I am having issues when i am trying to click and drag to generate a floor.

    My Debug.Log is only registering the first position that my mouse is clicking on and only changing that tile instead of all tiles in the area.

    Code (CSharp):
    1. // Start Drag
    2.         if (Input.GetMouseButtonDown(0)) {
    3.             dragStartPosition = currFramePosition;
    4.         }
    5.  
    6.         // End Drag
    7.  
    8.             if (Input.GetMouseButtonUp (0)) {
    9.             int start_x = Mathf.FloorToInt(dragStartPosition.x);
    10.             int end_x =   Mathf.FloorToInt(dragStartPosition.x);
    11.             if (end_x < start_x) {
    12.                 int tmp = end_x;
    13.                 end_x = start_x;
    14.                 start_x = tmp;
    15.             }
    16.  
    17.             int start_y = Mathf.FloorToInt(dragStartPosition.y);
    18.             int end_y = Mathf.FloorToInt(dragStartPosition.y);
    19.             if (end_y < start_y) {
    20.                 int tmp = end_y;
    21.                 end_y = start_y;
    22.                 start_y = tmp;
    23.             }
    24.  
    25.             Debug.Log("start_y; " + start_y + " end_y: " + end_y);
    26.             Debug.Log("start_x; " + start_x + " end_x: " + end_x);
    27.             for (int x = start_x; x <= end_x; x++) {
    28.                 for (int y = start_y; y <= end_y; y++) {
    29.                     Tile t = WorldController.Instance.World.GetTileAt(x, y);
    30.                     if (t != null) {
    31.                         t.Type = Tile.TileType.Floor;
    32.                     }
    33.                 }
    34.             }
    35.         }
     
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Code (CSharp):
    1.  
    2. if (Input.GetMouseButtonDown(0))
    3. {
    4.    if (!buttonMousePressed)
    5.    {
    6.       // Start Drag
    7.       buttonMousePressed = true;
    8.       dragStartPosition = currFramePosition;
    9.    }
    10.    //here you can add a visual drag 2d rectangle if buttonMousePressed is true
    11. }
    12. if (Input.GetMouseButtonUp (0) && buttonMousePressed)
    13. {
    14.    //end drag
    15.    buttonMousePressed = false;
    16.    //your code here
    17. }
    18.  
     
  3. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    Thank you,

    This is now telling me that buttonMousePressed does not exist in this context.
     
  4. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Code (CSharp):
    1. private bool buttonMousePressed;
    2.  
    3. void Start()
    4. {
    5.    buttonMousePressed = false;
    6.    //rest of your code
    7. }
     
    Milambur88 likes this.
  5. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    Thanks this is still giving me that same issue it is only changing the first tile that is clicked instead of the whole area that is dragged across.

    this is the tutorial i am using the part i am trying to do starts at 24:14

     
  6. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Ahh... tutorials!
    You need to search algorithm and then translate the algorithm into code.

    I cannot help you, maybe you've skipped some lines of code.