Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How can my blocks be selected by sliding over them rather than needing to be individually clicked

Discussion in 'Input System' started by kayrayorulmaz, May 17, 2021.

  1. kayrayorulmaz

    kayrayorulmaz

    Joined:
    Jul 23, 2020
    Posts:
    3
    My game is about selecting blocks. The function I use to input this is OnMouseDown. Once clicked, a series of if statements are used to see if that block can be selected.

    However, the only way for the block to be recognized that it is being clicked is if I went over it and clicked it. Since the game is about selecting these blocks quickly, individually tapping every block could get very tedious very quickly.

    I tried to use the OnMouseDrag() function but that kept on selecting and deselecting blocks on every frame.

    What can I use to allow my blocks to be selected when slid over AND individually selected?

    Here is the Minimum reproducible code:

    Code (CSharp):
    1. void OnMouseDown()
    2.      {
    3.          if (canBeSelected)
    4.          {
    5.              if (!selected && SelectedBlocks.Count <= 6)
    6.              {
    7.                  if (firstSelect && gameObject.tag != "Operation" && SelectedBlocks.Count < 1)
    8.                  {AddBlock();}
    9.              else if (!firstSelect && SelectedBlocks.Count >= 1)
    10.              {
    11.                  if(SelectedBlocks[SelectedBlocks.Count - 1].tag != gameObject.tag)
    12.                  {
    13.                      if ((int)gameObject.transform.position.x <= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.x + 1 && (int)gameObject.transform.position.x >= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.x - 1)
    14.                      {
    15.                          if ((int)gameObject.transform.position.y <= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.y + 1 && (int)gameObject.transform.position.y >= (int)SelectedBlocks[SelectedBlocks.Count - 1].transform.position.y - 1)
    16.                          {AddBlock();}
    17.                      }
    18.                  }
    19.              }
    20.          }
    21.          else if (selected)
    22.          {
    23.              SpawnBlocks.RemoveBlock(myID, false);
    24.              audioData.PlayOneShot(removeAudio);
    25.          }
    26.      }