I am trying to raycastall from the center of the player, and if the ray hit more than once it should draw a white line, if only once then draw a green line. It performs fine with two separate colliders, but when I created a S shaped edge collider 2d, this ray returns only 1 earliest hit with the edge collider. Is there a way to let the ray know it should collide multiple times with one single collider?
Time to start debugging! Find out what that raycast is actually hitting… printing the names of the objects to Debug.Log() is probably the easiest debugging step to start with. Then make it longer, put other things in front of it you KNOW it will hit, etc.
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
According to this the thread below, you cannot make RaycastAll return multiple hits for the same collider.
The thread suggests that you could manually spawn a new ray near the point of the first hit to see if you get another hit. Raycasts usually ignore the colliders if the ray starts in the collider, but I don’t know how it will behave if it starts in the collider, leaves it, then re-encounters it. It might be worth some experimentation.
No but if you think about it, that wouldn’t make sense. If you hit a collider, you are also hitting it continually until you exit the other side. If instead you want the queries to “pretend” that colliders are somehow “hollow” and only detect the edge intersections then that isn’t how the physics engine works. You only get a single hit per collider for a single query. RaycastAll returns all the colliders along the ray but any collider will only appear once.
Of course, you are free to perform a single Raycast then perform another a tiny distance further on but you’d need to convert your colliders to edges only i…e open shapes. You can do this by adding them to a CompositeCollider2D and setting it to “Outline” mode. The thing is then that these are edges, there is no inside so it’ll work for your queries but stuff won’t get pushed out from inside it because it’s still an open (not closed) shape.
Also, avoid the “All” suffixed queries, they are terrible for performance as they create an array for you which then gets left to the GC. To avoid this, every single 2D physics query allows you to pass an array or more preferably a List (which can grow) to accommodate the results.
In 2D you can control whether you get results when starting inside a collider with the following global option: https://docs.unity3d.com/ScriptReference/Physics2D-queriesStartInColliders.html