- Home /
2D alternative to Physics.CheckSphere
Hi,
I'm trying to convert the following method from a pathfinder script to something that would work in a 2D game with 2D objects.
void CreateGrid() {
grid = new Node[gridSizeX,gridSizeY];
Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x/2 -
Vector3.forward * gridWorldSize.y/2;
for (int x = 0; x < gridSizeX; x ++) {
for (int y = 0; y < gridSizeY; y ++) {
Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter +
nodeRadius) + Vector3.forward * (y * nodeDiameter + nodeRadius);
**bool walkable = !(Physics.CheckSphere(worldPoint,nodeRadius,unwalkableMask));**
grid[x,y] = new Node(walkable,worldPoint, x,y);
}
}
}
I have tried to use;
void CreateGrid()
{
grid = new Node[gridSizeX, gridSizeY];
Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - new Vector3 (0,1,0) * gridWorldSize.y / 2;
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) +
new Vector3 (0,1,0) * (y * nodeDiameter + nodeRadius);
**BoxCollider2D bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
bool walkable = !(Physics2D.IsTouchingLayers(bc, unwalkableMask));**
grid[x, y] = new Node(walkable, worldPoint, x, y);
print(walkable);
}
}
}
currently bool walkable is returning true ( or false if i remove !) so the bc boxcollider is not touching unwalkable layer.
I'm well out my depth here. Trying to convert a pathfinder script to 2D. I can give more info but I need some help if poss. Thanks.
Answer by Bunny83 · Jul 28, 2018 at 10:30 PM
The 2d equivalent of CheckSphere / OverlapSphere would be OverlapCircle. The 2d physics system doesn't have a CheckCircle method. However you just have to check the return value of OverlapCircle against null
Answer by wsimm101 · Jul 28, 2018 at 11:46 PM
Thanks so much. I tried;
void CreateGrid()
{
grid = new Node[gridSizeX, gridSizeY];
Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - new Vector3 (0,1,0) * gridWorldSize.y / 2;
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + new Vector3 (0,1,0) * (y * nodeDiameter + nodeRadius);
//BoxCollider2D bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
box = new Vector2(nodeDiameter - 0.1f, nodeDiameter - 0.1f);
bool walkable = !(Physics2D.OverlapBox(worldPoint, box, 90, unwalkableMask));
//bc.transform.position = worldPoint;
grid[x, y] = new Node(walkable, worldPoint, x, y);
print(box);
}
}
}
and it worked nicely. just gotta get my player to follow the path now! The full script is here if any one is interested.
https://github.com/SebLague/Pathfinding/tree/master/Episode%2003%20-%20astar/Assets/Scripts
I don't seem to be able to make this work for a 2d top down game. As you, I've also changed the worldBottomLeft vectors to be (0,1,0) as well as used OverlapBox ins$$anonymous$$d of OverlapCircle, however the nodes keep showing up as walkable (the grey zone, ins$$anonymous$$d of showing up red):
Screenshot: http://cld.wthms.co/6qArZ0
$$anonymous$$y terrain is based in 2 types of tiles: - Floor, layer: default, does not have box collider 2d - Walls, layer: unWalkable, has box collider
Any idea?
Edit: Fixed - The problem was that I was calling the CreateGrid() method on Start(), but in my game the levels are procedurally generated and are not finished until later in execution. $$anonymous$$oving the method call to later on made it work just fine.
There is also the method;
public Node NodeFromWorldPoint(Vector3 worldPosition)
in which the following needs to be changed from;
float percentY = (worldPosition.z + gridWorldSize.y / 2) / gridWorldSize.y;
to;
float percentY = (worldPosition.y + gridWorldSize.y / 2) / gridWorldSize.y;
there might be one other bit that needs changing to, can't remember.