- Home /
How to Check if any Children of a GameObject are in a specific area.,Check if a position contains a GameObject or children of a GameObject
I need to create a platform in a area, but need to check that there is nothing there first. All of the objects that I need to check for are all children of one GameObject. How would I check that none of them are in the area? (They all have BoxCollider2D components),I want to check that there are none of a specific GameObject in a specific position, represented by a Vector3. How would I check for the object's location? (All the Object's I'm checking for will have non-trigger BoxCollider2D components.)
You need to provide more information about your came.
Is it based on a grid system?
What shape is the platform?
Can you not just place the new object without the mesh being visible, check for a collision and if there was one remove it else enable the mesh on the next frame?
Platforms are just squares, 64x64. If the code snippet below doesn't work, I'll give your idea a shot. I don't know how you mean grid system, if you mean in design yes, but if there is any specific grid system in unity I am not aware of it, and wish I was when I started making the game.
Answer by YetAnotherKen · Aug 24, 2019 at 08:06 PM
This code snippet comes right out of one of my own scripts:
RaycastHit hit;
Vector3 observer = new Vector3(x + xOffset, 1000, y + yOffset);
if (Physics.Raycast(observer, Vector3.down, out hit)) //Shoot a raycast down from on high to locate the position of the mesh where the model will go
{
if (hit.rigidbody == null) // this makes sure there is not a object below this one...happens
{
vertexHeight = hit.point.y;
}
I use this to make sure I don't spawn one object on top of another.
Oh, to clarify, the objects I am instantiating have rigidbodies on them so that characters cannot walk right through them.
Okay, Will Try and implement that into my code, however it will take a while for me to get back to you on if I had success or not.