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

ContactFilter2D and Physics2D.Raycast

Discussion in 'Physics' started by Alunze, May 7, 2018.

  1. Alunze

    Alunze

    Joined:
    Jun 2, 2013
    Posts:
    11
    Hello!

    There are basically two flavours of the Physics2D.Raycast function:

    - Without a ContactFilter2D filter, in which case the result is a single ray.
    - Using a ContactFilter2D filter, in which case the result is an array (of configurable size) of rays fulfilling the filter.

    I just want to get the ray to the closest surface that meets the criteria in the filter (which is a convenient construct), so my question is, if I use the filtered version, can I trust the returned rays are ordered by distance? . If I supply an array with a single position, would rays[0] be the shortest ray?

    Thanks in advance!
     
    Deeeds likes this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    It's irrelevant which version you use, all raycast results are sorted so first element is first along the ray, second is further etc.

    Hope that helps.
     
    Last edited: May 8, 2018
    Deeeds likes this.
  3. Alunze

    Alunze

    Joined:
    Jun 2, 2013
    Posts:
    11
    That's exactly the information I needed. Much appreciated!