I am making tower defense game and to avoid enemies going through each other, I use a raycast to determine the distance between them. For now, everything works fine, but I want to make faster enemies able to go through the slower (so only check the distance for similar enemies). The problem is that I have more enemies types than layers and enemies are all in the same path (so raycast is often intercepted by the wrong enemy and then stops. I want to find a way to make it go through).
Situation example :
enemy 1 --- enemy 2 --- enemy 3
enemy1 shoots a ray to calculate the distance between him and enemy3, but the ray is intercepted by enemy2.
How do I make ray go through enemy2 if the enemy that fires it is not the same type ?
I hope my problem is clear enough, if not please tell me. Thanks in advance.
For 2D Physics, Physics2D.Raycast has multiple overloads, some of which take an array or a list of RaycastHit2D as a parameter which the function will promptly fill.
For 3D Physics, Physics.RaycastAll will return an array of RaycastHit. Note that the results of this function won't be in order, so you may want to sort the resulting list by distance from the ray origin.
Actually, upon closer inspection, there's also a Physics2D.RaycastAll that I didn't know about. I've been using the overloads of Physics2D.Raycast all this time. You learn something new every day.