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. Dismiss Notice

Question Physics2D.Raycast() does not hit CompositeCollider2D

Discussion in '2D' started by sasuchi, Oct 8, 2020.

  1. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Hello Everyone,

    I am currently facing the following issue:
    I have a single Tilemap composed of Tiles, where some Tiles (e.g.ground) dont have colliders and some Tiles do have colliders (e.g. wall/map-limitter). The player has a gameobject, called focus, that will selects one tile adjacent to the player, corresponding to the mouse position, and with Input.GetMouseButtonUp(0) it does a Physics2D.Raycast() and should act accordingly.
    With the CompositeCollider2D activated, the fence/Tile with a collider is not being hit.
    With the CompositeCollider2D deactivated (hence using single colliders), the fence/Tile with a collider is being hit.

    Code (CSharp):
    1.  void UseAction(Vector2 point)
    2.     {
    3.         focus.SetActive(false);
    4.         RaycastHit2D hit = Physics2D.Raycast(focus.transform.position, Vector2.zero, Mathf.Infinity);
    5.         if (hit.collider != null && hit.collider.gameObject != gameObject)
    6.         {
    7.             Debug.Log(hit.collider.name);
    8.         }
    9.         else
    10.         {
    11.             GameManager._instance.tilemap.activeMap.Interact("Fist", new Vector2Int(Mathf.RoundToInt(point.x), Mathf.RoundToInt(point.y)));
    12.         }
    13.     }
    Those are the ColliderSettings at the Tilemap
    upload_2020-10-8_12-12-16.png

    Response with inactive CompositeCollider2D :
    upload_2020-10-8_12-13-25.png

    And obviously there is no response when its active, instead the tile is swapped.

    Are there any Solutions beside not using the CompositeCollider2D, since I dont want any phantom vertices at the map-delimiter, or using multiple Tilemaps ?

    I appreciate your input.

    best regards
    sasuchi
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    No idea
    This isn't a raycast at all. It's a degenerate ray trying to do an overlap point because it has no direction but infinite distance. It makes little sense to do that when there's a dedicated call to do it i.e. Physics2D.OverlapPoint

    I think the problem you're having though is that you're assuming that an outline made of edges somehow has an inside which it doesn't. It's not a polygon closed shape, it's an open shape composed of edges.

    You can only detect edges by intersecting the edges i.e. crossing the edges with a raycast.

    This is the same for CompositeCollider2D in Outline mode as it is for an EdgeCollider2D because they both produce the same primitive edge chains in the physics system.
     
    sasuchi likes this.
  3. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Thank you for the Reply MelvMay,

    Concerning you're point about the OverlapPoint, yes you are absolutely correct, I must have overlooked that function while looking into the documentation or simply was already satisfied because what i tested worked with the ray :).

    However concerning the edge detection, the given input Vector2 point is rounded to Int values and the Tile colliders are set to Grid Colliders. Hence the detection should only miss, if the corner that is pointed to is not existent.

    As example, the Vector point is in both cases (0,0,0) (the slightly greyish sprite with a size of 1 is the gameobject which transform is rounded to nearest int).
    On the left image its without the compositeCollider2d and on the right its with the compositeCollider2d

    upload_2020-10-8_15-19-4.png

    I would assume it would look for overlapping in the lower left corner (EDIT or maybe upper right, since 0.5f would probably be rounded to 1, if i think about it)
    upload_2020-10-8_15-23-8.png
    and not, as you also mentioned, the center of the gameobject, because then it wouldn't detect the overlapping point in neither scenario (with/without CompositeCollider).

    I hope I could make my point clear and unterstood from you completly, since Im not a native speaker.

    Thanks
     
    Last edited: Oct 8, 2020
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    As I said, you cannot detect overlap with edges no matter how you use it. Edges are infinitely thin and have no area to overlap with. It doesn't matter if you're using ints or whatever value you use. Only polygons, capsules & circles have an area that can overlap.

    For edges, you have to perform an intersection test that passes through an edge so line/ray cast that passes through it or shape-cast.

    If you cast a line from the player to the center of the tile then that will tell you if you will hit an edge or not.
     
  5. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Okay, but then, why does it detect the Overlap with the same exact code if I am not using a CompositeCollider ? Shouldn't it be unable regardless of the Collider of the Tile?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Because the TilemapCollider2D produces polygons which are closed shapes and do have an interior. If you set the CompositeColilder2D to Polygon it'll be the same. You've set the composite to Outline which uses edges the exact same as the EdgeCollider2D.
     
    Last edited: Oct 8, 2020
    sasuchi likes this.
  7. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Ah okay thank you very much now i've understood your point.