In 3D, Raycast is an easy and effective way to check if there's an open path. Code (CSharp): if (!Physics.Raycast(transform.position, inputDirection, 1.0f, wallLayerMask) { // Move } In 2D, Raycasts seem to work differently. Is there a simple alternative to 3D raycasts?
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
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.
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.
you probably want to disable "queries start in colliders" so that the behaviour is consistent with 3d physics raycast.
Just do the following if you're not interested in seeing the results, it's the same thing: Code (CSharp): void MyStuff() { if (Physics2D.Raycast(.....)) { // The stuff if we hit. } } Or if you want to see the results but also check if they're valid before you use them: Code (CSharp): void MyStuff() { var hit = Physics2D.Raycast(.....); if (hit) { // The stuff with the hit. } }
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.