- Home /
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[i].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[i].GetComponent<SpriteRenderer>().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 :)
So I'm back. I found a 2 ways but I couldn't get them work. The first is a pseudocode I found here (go in "Smoothing the A* Path" section and you'll find it), I don't know how to "translate" in C# the while part and also the ->next but I think it means i+1 in a for... The second way is something that came to my $$anonymous$$d some time ago, it works like this.
-The first node of the closed list cast a ray towards all the other nodes
-if the ray collides with something that is not a node add the last node to another list (wich will be the list of our final path)
-else repeat
I made an implementation of this:
GameObject check_node = closed_list.First();
for (int i = 0; i < closed_list.Count; i++)
{
RaycastHit2D[] ray = Physics2D.RaycastAll(check_node.transform.position, closed_list[i].transform.position, $$anonymous$$athf.Infinity);
for (int j = 0; j < ray.Length; j++)
{
if (ray[j].collider != null && ray[j].collider.gameObject.tag != "node")
{
if (j - 1 > 0)
{
if (!smooth_nodes.Contains(ray[j - 1].collider.gameObject))
{
if (ray[j - 1].collider.gameObject.tag == "node")
{
smooth_nodes.Add(ray[j - 1].collider.gameObject);
check_node = ray[j - 1].collider.gameObject;
}
}
}
else
{
if (!smooth_nodes.Contains(ray[j].collider.gameObject))
{
if (ray[j].collider.gameObject.tag == "node")
{
smooth_nodes.Add(ray[j].collider.gameObject);
check_node = ray[j].collider.gameObject;
}
}
}
}
}
}
but it doesn't work. What should I do?
Thank you very much guys for your help and time
Answer by Dutchysaurus · Jul 03, 2016 at 06:25 PM
Hey there. I typed up a response to this and then accidentally refreshed the page. Fun :P
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[i]. 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[i].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[i].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[i].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[i].layer = 1;
//"closed_list[i+1].layer = 1;" If the (i+1) node wasn't removed, reset it's layer
}
closed_list[i].GetComponent<SpriteRenderer>().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.
Hi, yes me too, I pressed accidentaly backspace while writing the question and it deleted everything. Unity should really make an autosave system. In my code I took in account also the layers, like you wrote but it didn't work. I don't think the problem is with the layers or the raycast that collides with the collider of the gameobject that casted it because in Project Settings > Physics2D I disabled "queries start in colliders" and I also tried to make the default layer (the layer that contains everything) and the node layer (the layer to use in the for,it's the number 8) ignore the collision between themselves but it didn't work. Anyway thank you very much for your help :D.
If it's not that, I would go through and chuck debugs all through the code to see everything working in action. Double checking all the nodes have colliders to hit. That's all I got, good luck.