Search Unity

Question Alternatives to Raycast

Discussion in '2D' started by ranjansikand, Jan 19, 2022.

  1. ranjansikand

    ranjansikand

    Joined:
    Apr 8, 2021
    Posts:
    110
    In 3D, Raycast is an easy and effective way to check if there's an open path.

    Code (CSharp):
    1. if (!Physics.Raycast(transform.position, inputDirection, 1.0f, wallLayerMask) {
    2.     // Move
    3. }
    In 2D, Raycasts seem to work differently. Is there a simple alternative to 3D raycasts?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Raycast does absolutely NOTHING regarding "open paths," whatever that is.

    Raycast detects colliders, a single collider, or the intersection with a Plane.

    That's it. Don't think of it as doing anything like pathing!!

    The function interface / API design is slightly different.

    The idea and intention is identical.

    What part are you struggling with?

    Finally, in summary, raycasting, colliders, planes, Plane, etc:

    https://forum.unity.com/threads/hel...s-are-hitting-an-object.1058393/#post-6840296

    https://forum.unity.com/threads/kee...within-the-view-frustrum.936209/#post-6117173

    https://forum.unity.com/threads/ran...ear-polygon-collider-2d.1060082/#post-6851372
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    In what way?

    I mean, there are docs here.
     
  4. ranjansikand

    ranjansikand

    Joined:
    Apr 8, 2021
    Posts:
    110
    Well, that's exactly what I meant. It's easy to check if there's an open path because a raycast will return true if there's something in the way. In the Physics2D library, they all seem to return Collider2D rather than bool.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Physics2D.Raycast does NOT return a Collider2D. It returns a RaycastHit2D, which MAY have a Collider2D reference inside of it.

    The presence or absence of the .collider reference performs the exact function of the bool in the 3D physics.

    You should definitely make sure you look at the sample code. None of this needs to be mysterious. :)
     
  6. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    231
    you probably want to disable "queries start in colliders" so that the behaviour is consistent with 3d physics raycast.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Just do the following if you're not interested in seeing the results, it's the same thing:
    Code (CSharp):
    1. void MyStuff()
    2. {
    3.    if (Physics2D.Raycast(.....))
    4.    {
    5.       // The stuff if we hit.
    6.    }
    7. }
    Or if you want to see the results but also check if they're valid before you use them:
    Code (CSharp):
    1. void MyStuff()
    2. {
    3.    var hit = Physics2D.Raycast(.....);
    4.    if (hit)
    5.    {
    6.       // The stuff with the hit.
    7.    }
    8. }
     
    ranjansikand likes this.
  8. ranjansikand

    ranjansikand

    Joined:
    Apr 8, 2021
    Posts:
    110
    Thanks, I'll try this again. I had essentially done this, since I copied my code from my 3D project over, but it wasn't working as intended.