RayCastAll seemingly ignoring a particular collider
Hello everyone, I'm implementing an A* algorithm for a 2D point&click, based on Nodes. This is what I'm using as reference: http://www.jgallant.com/nodal-pathfinding-in-unity-2d-with-a-in-non-grid-based-games/
I have instantiated a two dimensional array of PathNodes (Prefab holding a PathNode component) that each have a Circle2D Collider on a Phsyic Layer called "NavMeshNode". They are placed inside the scene, roughly on the "Walkable Ground" gameObject's collider.
Right now I am trying to instantiate the connections between each Node and it's potential 8 neighbours. I do not want to make connections between 2 Nodes that are separated by "Not Walkable" Ground (another GO holding only a collider).
This is the code I am using to Raycast in all 8 directions (called on each Node):
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0) continue;
RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position , new Vector2(i, j), nodeDist);
print(hits.Length + " Hits!");
I am then iterating through the hits
Array, to check for "Not Walkable" and "NavMeshNode" Colliders hit.
bool notWalkableGroundFound = false;
PathNode nodeHit = null;
foreach (RaycastHit2D hit in hits)
{
LayerMask l = hit.collider.gameObject.layer;
print("Hit: " + LayerMask.LayerToName(l));
if(l == LayerMask.NameToLayer("NotWalkable"))
{
notWalkableGroundFound = true;
break;
} else if(l == LayerMask.NameToLayer("NavMeshNode"))
{
nodeHit = hit.collider.GetComponent<PathNode>();
}
}
if(!notWalkableGroundFound && nodeHit != null)
{
if (neighbours == null) neighbours = new List<PathNode>();
neighbours.Add(nodeHit);
}
And here's my problem: I am hitting EVERY Collider in that area (Phsyic Layers "Walkable", "Clickable" (Ground that can be clicked, but not walked), "Not Walkable") but not a single NavMeshNode Collider (or even "Default").
So apparantly, RaycastAll() is somehow not detecting the Colliders of my NavNode GameObjects.
Some things I tried to fix it:
Call the RaycastAll() method from another GO (currently it's being called on each of the Nodes)
Add an offset to the raycast position, to make it start outside of its own NavMeshNode collider
Changed the z-Positions and Phsyic Layers in various ways
Gave the parent GO of the nodes a collider (this gets detected)
PLEASE someone help me with this, I am really out of ideas... Thanks a lot in advance!!
Your answer
Follow this Question
Related Questions
Stucking with RTS Building detection using 2D Raycasts and 2DBoxColliders 0 Answers
Select all layers in the scene except the triggers 0 Answers
RaycastAll from prefab is not picking up a collider from another prefab 1 Answer
How to trigger functions like OnTriggerEnter2d and OnTriggerExit2d with raycasthit2d ? 1 Answer