A* Pathfinding Project

Unable to Place a blocking object on a node that is already occupied


In my game I allow the player to place rocks that block the path. This works as I expect.

But I also want to allow the player to place new objects on top of the previous rocks in order to replace them. To do this I am using this code collect the mouse position and snap the attached object to the grid:

 private void MoveCurrentObjectToMouse()
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if (Physics.Raycast(ray, out hitInfo))
            currentPos = new Vector3(hitInfo.point.x, 0.5f, hitInfo.point.z);
            NNInfo nearestNodeInfo = AstarPath.active.GetNearest(currentPos + offset, NNConstraint.Default);
            if (nearestNodeInfo.node != null)
                currentPlaceableObject.transform.position = (Vector3)nearestNodeInfo.node.position - offset;
                currentPlaceableObject.transform.position = new Vector3(currentPlaceableObject.transform.position.x, (0.25f), currentPlaceableObject.transform.position.z);

But when I try to actually use the mouse to place the object on top of the previous rock it skips the used node an snaps to the unoccupied node on the other side.

I’d appreciate any assistance you can provide with this issue.


It’s being filtered cause you set the constraint to default. Which checks if nodes are walkable.
Try this:

NNInfo nearestNodeInfo = AstarPath.active.GetNearest(currentPos + offset, NNConstraint.None)