RaycastHit2D not detecting a specific gameobject in a for (for path smoothing in A*)

Hello all,I’m making a 2d top down game and I’m making an A* AI for the enemies based on a node system. I made a very simple function too smooth the path found by reducing the number of nodes to follow, given the node i if i “sees” i+2 delete i+1. that’s the code:

void SmoothPath()
    {
        for(int i=0;i<closed_list.Count;i++)
        {
            if (i + 2 < closed_list.Count)
            {
                RaycastHit2D ray = Physics2D.Raycast(closed_list*.transform.position, closed_list[i + 2].transform.position,*

Mathf.Infinity);

if (ray.collider != null)
{
if (ray.collider.gameObject==closed_list[i+2])
{
closed_list.Remove(closed_list[i + 1]);
Debug.Log(ray.collider.gameObject.name);
}
}
}
closed_list*.GetComponent().color = d;
_
}_
_
}*_
The problem is that the if is never true even if i+2 has surely been hit because in this list of nodes (closed_list) every node, as you can see in the cycle, casts a ray towards the node after its adjacent node. But it doesn’t execute the if. Instead if I use ray.collider.gameObject.tag=="node" the if is executed but it doesn’t do what it should do. I also tried with GetInstanceID() but nothing. I’m pretty sure it’s a stupid logic error I’m making. What’s wrong? Thanks in advance :slight_smile:

Hey there. I typed up a response to this and then accidentally refreshed the page. Fun :stuck_out_tongue:

First things first, you for loop can be combined with your first if statement to make things a bit clearer:

for (int i = 0; i + 2 < closed_list.Count; i++)

Anywho. I believe your problem stems from the fact that your raycast begins inside of your node and thus is hitting this whenever it is drawn. This is why your
if (ray.collider != null)
statement is returning true, while
if (ray.collider.gameObject==closed_list[i+2])
is returning false. This is because you are always hitting closed_list*.*
To fix this, you simply need to incorporate layer masks, and assigning them during the loop so that the raycast can ignore the originating object and hit everything else. To do this, you have to setup a new layer, for example, “Originating Node”. If this is your only layer, make it layer number 8, otherwise, the next available layer is fine. Next comes the code

void SmoothPath()
{
//Simplified for loop
for (int i = 0; i + 2 < closed_list.Count; i++)
{
//Adjusting the node we start from to be ignored by the raycast.
closed_list*.layer = 8;
//For completions sake, you could also add “closed_list[i+1].layer = 8” to this*

//line, which would make sure that if the (i+1) node is inbetween the raycast,
//it will be ignored

//The only Adjustment here is adding the layermask ‘~(1 << 8)’ Link for layermasks is
//down below. This will allow our raycast to pass through only objects that are on
//this layer, and hit everything else.
RaycastHit2D ray = Physics2D.Raycast(closed_list*.transform.position, closed_list[i + 2].transform.position,Mathf.Infinity, ~(1 << 8));*

if (ray.collider != null)
{
if (ray.collider.gameObject == closed_list[i + 2])
{
//We no longer need to ignore the originating node (i) so we can reset
//it’s layer to the default so that we don’t mess up further iterations
closed_list*.layer = 1;
//“closed_list[i+1].layer = 1;” reset the nodes layer*

closed_list.Remove(closed_list[i + 1]);
Debug.Log(ray.collider.gameObject.name);
}
}
//At the end of each loop, resetting to the default layer
closed_list*.layer = 1;
//“closed_list[i+1].layer = 1;” If the (i+1) node wasn’t removed, reset it’s layer*

}
closed_list*.GetComponent().color = d;
_}*
*You can read about what i’ve done with the layers here: https://docs.unity3d.com/Manual/Layers.html*_

I hope that solves your problem, any follow ups, feel free to send them through.