Question by
yoelleoy · Oct 08, 2020 at 04:05 PM ·
programmingphysics2d
Help with Physics2D.OverlapBox
I have a script that populates a 2D grid with objects, I am trying to add a function that will check if there is already an object on the grid space using Physics2D.OverlapBox but no matter what I do, the check returns true. I have double checked that the objects that I am checking for are all in layer 8 have have a 2D box collider on them.
{ int layerMask = 1 << 8; //Layer 8
// Rows and cols for the map grid
public Vector2 myGrid;
// Prefabs dimensions in Unity units
public Vector3 tileDimensions;
// Populate with all the prefabs used to generate map
public GameObject[] prefabTiles;
void Start()
{ CreateMap();}
void CreateMap()
{{ for (int row = 1; row <= myGrid.y; row++)
{ for (int col = 1; col <= myGrid.x; col++){ {
if (Physics2D.OverlapBox((new Vector2((col - 1) * tileDimensions.x, (row - 1) * tileDimensions.y)),(new Vector2(1f,1f)), layerMask) == false)
{ Debug.Log("FreeSpace");
int n = Random.Range(0, prefabTiles.Length);
GameObject theTile = Instantiate(prefabTiles[n], transform);
theTile.transform.localPosition = new Vector3((col - 1) * tileDimensions.x, (row - 1) * tileDimensions.y, 1);
}
}
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Grapling gun help! Why doesn't my player move towards? Instead of getting stuck in place 0 Answers
implementing a wall jump 1 Answer
How could I add "gravity" to a top-down shooter's bullet, such as in The Binding of Isaac? 0 Answers
PROBLEM IN MOVING WITH C # 1 Answer
Question about Input.GetKey 1 Answer