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;
}
}
}
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.
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.
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:
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.