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

Problem with object detection using mouse click in 2D isometric view

Discussion in '2D' started by ShieldBlock, Apr 7, 2020.

  1. ShieldBlock

    ShieldBlock

    Joined:
    Sep 8, 2018
    Posts:
    4
    Hi,

    I'm working on a 2D isometric game. I have my tiles set up to where to collider is only at the base of the objects( for layering and character movement purposes).

    This is a problem because when I try to click on the object, it only gets detected if I press where the collider is drawn.

    Is there a workaround for this, so I can detect the object no matter where I click, but keep the collider at the base.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    "Click on the object" could mean anything. What do you mean by this? What do you mean by "collider is drawn"? Are you talking about the colilder gizmo?

    Colliders are there for physics so I'm trying to understand what you're asking from a physics point-of-view but it sounds like you're not talking about physics, queries etc but some editor function.
     
  3. Entapsia

    Entapsia

    Joined:
    Aug 2, 2016
    Posts:
    3
    Hi,

    I suppose you want your objects to collide only from the base, but the complete object to be clickable.

    So, try to use a collider for the base of the object, and another one for the whole object, and make it trigger.

    Another way is to create a child of the object with only a collider component, and to change its layer, and layer collision parameters.
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Hi, you could solve this problem in the following way. It is not as easy as using colliders, but to my knowledge you cannot associate a tile with multiple colliders.
    1. Convert from Screenspace (Input.mousePosition) to Worldspace
    2. Convert from Worldspace to Cellspace (the Grid component's WorldToCell).
    3. Get the tiles below the click position. Here, you know how far down you reasonably need to go given your art. In other words, your know that the tallest objects are only for example about 3 tiles high and are at max a few tiles wide. So, collect the tiles in this region below the click. The objective is to make sure, given that the tile's base is somewhere below the click, you can obtain all tiles in that area because one of them might be what was clicked at its top.
    4. Get current sprite from the tiles.
    5. Get the bounds of the sprites. This will work better for art that's squarish b/c bounds are a cube (or rectangular for 2D). This step could also be replaced with using some more complex structure to describe the precise shape of the tile. You could perhaps get the custom outline / physics shape assign in SpriteEditor if that's necessary.
    6. Adjust those bounds as if the sprite is positioned in the cell the tile is located in worldspace.
    7. Check if the worldspace click position is within the worldspace bounds. If not, discard this tile/sprite. It was not clicked neither near the base nor at its top.
    8. We might imagine many tall objects are in front of one another with only their peaks visible. The correct one with be the one that's closest to the camera. That's the one that most in front of all the others and was clicked. Depending upon the layout of your tilemap this might mean you need to pick the one with the smallest y-value for its worldspace position.
    Another simpler way to solve this problem: convert that tile to a GameObject so you can associate it with multiple colliders. If, however, you would need to convert essentially all your tiles to GameObjects you'll lose the benefits of using Tilemap, so there's a pro (simpler) and con (performance, memory).
     
    Last edited: Apr 8, 2020