- Home /
Checking for dead ends with Physics.OverlapBox()
Unity version: 2019.4.11f1
Hi, I'm having an issue trying to detect dead-ends in my game board generation. So far steps 1 to 6 work as intended, and yield a random board every time, comprised of tiles and paths generated at random, like so:
Now what I'm trying to do with step 7b is: for each path that isn't inactive, check if it's a dead end.
The way I'm trying to go about this is: for each path that isn't inactive, add a OverlapBox() at each extremity that counts the number of active game objects. If that number is one (meaning it found only the path it's checking from), then it's a dead end.
When I run that part of the script though, I get this:
What I suspect is the problem (idk if it really is that) is that instead of creating the boxes A and B at +/- y relative to the game objects, it's adding or subtracting y relative the the world. Again, it could be something else entirely, I'm really not sure.
Here's the method called by step "7b - Remove Dead Ends":
public void RemoveDeadEnds()
{
Debug.Log("RemoveDeadEnds() started");
// maybe I need to make everything active at the start (and then deactivate untagged at the end)?
// for each path that isn't untagged, get their hitCollisions
for (int i = 0; i < allPaths.Count; i++)
{
if (!allPaths[i].gameObject.CompareTag("Untagged"))
{
// this part is fucked, I don't think it places the boxes at the correct position. I suspect it adds the new Vector3() +/- y relative to the world, instead of the gameobject
Collider[] hitCollidersA = Physics.OverlapBox(allPaths[i].gameObject.transform.position + new Vector3(0, allPaths[i].gameObject.transform.lossyScale.y / 2, 0), new Vector3(0.4f, 0.4f, 0.8f)); // removed: allPaths[i].gameObject.transform.localRotation
Collider[] hitCollidersB = Physics.OverlapBox(allPaths[i].gameObject.transform.position + new Vector3(0, -allPaths[i].gameObject.transform.lossyScale.y / 2, 0), new Vector3(0.4f, 0.4f, 0.8f)); // removed: allPaths[i].gameObject.transform.localRotation
if (hitCollidersA.Length == 1) //I don't understand if it should be 1 or 2 help (en vrai ils ont pas l'air de se voir eux-même donc 1 paraît correct)
{
Debug.Log("RemoveDeadEnds() - " + allPaths[i].gameObject.name + ": DEAD END (hitCollidersA)");
for (int n = 0; n < hitCollidersA.Length; n++)
{
Debug.Log("hitCollidersA[" + n + "] = " + hitCollidersA[n].gameObject.name);
}
allPaths[i].gameObject.tag = "DeadEnd";
allPaths[i].gameObject.GetComponent<MeshRenderer>().material.SetColor("_Color", Color.yellow);
}
if (!allPaths[i].gameObject.CompareTag("DeadEnd"))
{
if (hitCollidersB.Length == 1)
{
Debug.Log("RemoveDeadEnds() - " + allPaths[i].gameObject.name + ": DEAD END (hitCollidersB)");
for (int n = 0; n < hitCollidersB.Length; n++)
{
Debug.Log("hitCollidersB[" + n + "] = " + hitCollidersB[n].gameObject.name);
}
allPaths[i].gameObject.tag = "DeadEnd";
allPaths[i].gameObject.GetComponent<MeshRenderer>().material.SetColor("_Color", Color.yellow);
}
}
if (!allPaths[i].gameObject.CompareTag("DeadEnd"))
{
Debug.Log("RemoveDeadEnds() - " + allPaths[i].gameObject.name + ": skip");
}
// set newly deactivated path's tag to "Untagged" and color white
}
}
}
Thanks in advance for any help or input!
Answer by CloodStool · Jan 05, 2021 at 04:24 PM
Aaaand I found the answer!
https://answers.unity.com/questions/1125044/how-do-i-move-an-object-relative-to-another-object.html
OP also found the answer themselves funilly enough ^^ instead of doing + new Vector3(0, allPaths[i].gameObject.transform.lossyScale.y / 2, 0)
I had use transform.TransformDirection() like so: + allPaths[i].gameObject.transform.TransformDirection(new Vector3(0, allPaths[i].gameObject.transform.localScale.y / 2, 0))
Your answer
![](https://koobas.hobune.stream/wayback/20220613022652im_/https://answers.unity.com/themes/thub/images/avi.jpg)