Search Unity

Colliders don't work on another collider

Discussion in '2D' started by Kyuubisaa, Jun 23, 2021.

  1. Kyuubisaa

    Kyuubisaa

    Joined:
    Jun 23, 2021
    Posts:
    4
    We are making a Hot-Cold game, where you need to select an object with a crosshair that answers a level question. We have three colors for fire: red is the correct answer, yellow is the answer is near, blue is the answer is far. We are trying to do this through several polygon colliders using tags,
    but they do not work adequately (another collider often responds). Please help with the solution of this issue.

    Script:


    public class PoligonButton : MonoBehaviour
    {
    public void OnMouseDown()
    {
    switch (this.tag)
    {
    case "MainTag": // This tag stay on elephant
    Debug.Log("on");
    break;
    case "NearTag": // This tag stay near elephant
    Debug.Log("near");
    break;
    default: // If far from the answer, untagged
    Debug.Log("off");
    break;
    }
    }
    }
     
    Last edited: Jun 24, 2021
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Nobody is going to know what that means TBH.

    There's not enough info here but this is neither 2D physics or a 2D feature; it's UI from what I can see.
     
  3. Kyuubisaa

    Kyuubisaa

    Joined:
    Jun 23, 2021
    Posts:
    4
    We have two objects with colliders, they are on top of each other. We need to handle clicks on these objects.
    Blue is above the red object and when you click on it, both objects are clicked.
    We want only blue to be clicked when blue is clicked. And when you clicked on red, only red was pressed, excluding the place,
    where the blue object lies. 123.jpg
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Just sounds to me like you should be using the actual UI system. You're already doing that as I see you're using Rect Transform.

    You seem to want to use the OnMouseDown UI callback then expect it to somehow know about what you want excluded etc. If you don't use real buttons in the UI then you'll need to deal with where the mouse is and determine yourself.
     
  5. Kyuubisaa

    Kyuubisaa

    Joined:
    Jun 23, 2021
    Posts:
    4
    The crux of the problem is that the collider lies on the collider. When you click, sometimes on the same point, both colliders can respond, they begin to get confused.
    More or less, the installation of a rigitbody on these objects saves the situation, but all the same, there are small places on the objects where a different collider can respond at the same point.
    And when setting the permission, something stops working when the game starts.
    We need to understand why two colliders, when they are on top of each other, start to conflict. And with and without rigitbody.
    We cannot use buttons and anything else, since our objects are not square, but polygonal, and we need just such a pressure range.
    We tried to find the pressed point in the following way:

    void Update ()
    {
    if (Input.GetMouseButtonDown (0))
    {
    Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.
    mousePosition);
    Vector2 mousePos2D = new Vector2 (mousePos.x, mousePos.y);

    RaycastHit2D hit = Physics2D.Raycast (mousePos2D, Vector2.zero);
    if (hit.collider! = null)
    {
    Debug.Log (hit.collider.gameObject.name);
    }
    }
    }

    And so, too, colliders start to get confused,
    even worse than the previous way.

    Maybe I'm not clear explaining the essence of the problem. If so, sorry, I'm Russian. :)
     
  6. Kyuubisaa

    Kyuubisaa

    Joined:
    Jun 23, 2021
    Posts:
    4
    Okay, moving along Z helped us so that all colliders are at different distances. This is the only thing that worked.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Why are the colliders confused? It's a 2D physics engine, there is no Z therefore they cannot and don't try to occlude each other. They are working as intended.

    This is a 2D physics engine so doing a degenerate raycast with zero length or direction isn't correct. You would use OverlapPoint for this. This will return BOTH colliders because they both overlap just like a raycast would return both if they both overlap the ray. Again, 2D physics (no Z depth) unlike 3D physics (with Z depth).

    You can perform a pseudo-3D raycast "in" or "out" of the screen but this is an illusion using GetRayIntersection but know that this is just a 2D raycast but when it gets the results from the 2D physics engine, it sorts them by the Transform.position.Z on the GameObject (know that this isn't done inside the physics engine). This means if you perform a raycast "in" then you'll get the closest Z along that ray as the first result so this can act as a type of occlusion but again, it's not something that 2D physics engine knows about.

    Sorry if I laboured the point of no Z depth but it's important to understand it's 2D meaning it only knows XY position and Z rotation and nothing else.

    Hope this helps.