Checking if GameObject is withing an other GameObject?
I am working on a tower defence game, ive got the pathing working. but what im having issue with is:
I am creating the nodes for the path finding script dynamical and what i am trying to do is to delete the nodes that appear inside a wall. i cant use trigger colliders since it kills the pathing thinking the nodes are blocking everthing.
Here is the code creating the nodes and then checking if its inside a wall:
private void CreateNodes(){
for(int z = 0 - (int)numNodesHorizontal / 2; z < numNodesHorizontal - (int)numNodesHorizontal / 2; z++)
{
for(int x = 0 - (int)numNodesLength/ 2; x < numNodesLength - (int)numNodesLength / 2; x++)
{
GameObject node = Instantiate(nodePrefab) as GameObject;
node.transform.parent = transform;
node.transform.localScale = new Vector3(1,1,1);
node.name = "Node: x: " + x + ", z: " + z + ".";
node.transform.localPosition = new Vector3(x, 5, z);
nodes.Add(node);
}
}
}
void DestroyNodes(){
for(int i = 0; i < nodes.Count; i++){
for(int w = 0; w < walls.Length; w++){
if(nodes[i].transform.TransformPoint(nodes[i].transform.localPosition).x - nodes[i].GetComponent<Collider>().bounds.size.x >= walls[w].transform.localPosition.x - (walls[w].GetComponent<Renderer>().bounds.size.x / 2) &&
nodes[i].transform.TransformPoint(nodes[i].transform.localPosition).x + nodes[i].GetComponent<Collider>().bounds.size.x <= walls[w].transform.localPosition.x + (walls[w].GetComponent<Renderer>().bounds.size.x / 2) &&
nodes[i].transform.TransformPoint(nodes[i].transform.localPosition).y - nodes[i].GetComponent<Collider>().bounds.size.y >= walls[w].transform.localPosition.y - (walls[w].GetComponent<Renderer>().bounds.size.y / 2) &&
nodes[i].transform.TransformPoint(nodes[i].transform.localPosition).y + nodes[i].GetComponent<Collider>().bounds.size.y <= walls[w].transform.localPosition.y + (walls[w].GetComponent<Renderer>().bounds.size.y / 2) &&
nodes[i].transform.TransformPoint(nodes[i].transform.localPosition).z - nodes[i].GetComponent<Collider>().bounds.size.z >= walls[w].transform.localPosition.z - (walls[w].GetComponent<Renderer>().bounds.size.z / 2) &&
nodes[i].transform.TransformPoint(nodes[i].transform.localPosition).z + nodes[i].GetComponent<Collider>().bounds.size.z <= walls[w].transform.localPosition.z + (walls[w].GetComponent<Renderer>().bounds.size.z / 2) ){
Destroy(nodes[i]);
}
}
}
}
As you see in the screen shot below it deletes only a few and not anywhere where im checking.
Did you try to store it in List And destroy it later?
They are stored in a list, and after they are created they are destroyed.
$$anonymous$$aybe try destroy it later? Invoke by a few seconds to destroy it?
its destroying some of them but not the right ones
Your answer
Follow this Question
Related Questions
Score won't work 1 Answer
Struggling with bounce pad in platformer game. 1 Answer
Best Way To Have An Enemy NPC Chat With Player 0 Answers
How do I load tiles at runtime? 0 Answers
What is the different between OnTriggerEnter and OnCollisionEnter ? 2 Answers