- Home /
Destroying all objects with a certain tag that ray cast if colliding with
I'm trying to destroy cubes that are touching each other. Each cube cast a ray that only extends long enough to collide with the next cube. I want to be able to destroy multiple cubes that are on top or next to each other that are the same when they are struck by an object. I tried to use a null stament so that when one cube is destroyed and it was touching another cube it would cause a chain reaction and they would be destroyed as well, but it did not work. This is my ray cast code so far.
public Rigidbody cube;
public GameObject cubeAray;
public float distanceUp=.23f;
public float distanceLeft=.95f;
public float distanceDown=.25f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
RaycastHit hit;
Vector3 fwd = cube.transform.TransformDirection(Vector3.forward);
Vector3 uwd= cube.transform.TransformDirection (Vector3.up);
Vector3 dwn=cube.transform.TransformDirection (Vector3.down);
Debug.DrawRay(cube.transform.position+Vector3.left*distanceLeft, fwd * .4f, Color.green);
Debug.DrawRay(cube.transform.position+Vector3.left*distanceUp+Vector3.up*distanceUp, uwd* .3f, Color.green);
Debug.DrawRay(cube.transform.position+Vector3.left*distanceDown+Vector3.down*distanceDown, dwn* .3f, Color.green);
//raycast hit UP
if(Physics.Raycast (cube.transform.position+Vector3.left*distanceUp+Vector3.up*distanceUp, uwd, out hit, .3f))
{
{
print ("UP UP");
}
}
//raycast hit left
if(Physics.Raycast (cube.transform.position+Vector3.left*distanceLeft, fwd, out hit, .4f))
{
if(hit.collider.tag=="PlayerCube" )
{
print ("SIDE SIDE");
}
}
//raycast hit down
if(Physics.Raycast (cube.transform.position+Vector3.left*distanceDown+Vector3.down*distanceDown, dwn, out hit, .3f))
{
if(hit.collider.tag=="PlayerCube" )
{
print ("UP UP");
}
}
}
}
What didn't work? The rays are wrong? Didn't collide? It collide with anything other that PlayerCube?
You can approach this in one of three ways:
Put a script similar to this one on every cube and have them check themselves.
Get a list of all the cubes and iterate through them running this same code
Use recursion. One you get one hit, you then apply this same code to the hit object as well. Note you'll want to change the tag to avoid looping.
I need all the objects that the raycast is touching to be destroyed when an object collides with one of the cubes and destroys it.
You are looking for a chain reaction. That is if one block is destroyed then any touching neighbors of that block are also destroyed. And Raycasting is how you detect the neighbors. Correct? If so, then the Recursion approach is best.
Pseudo code:
void CheckNeightborsAndDestory(Transform target) {
target.tag = "ToBeDestoryed";
if (Raycast left gets a hit) {
CheckNeightborsAndDestory(hit.transform);
}
if (Raycast right tets a hit) {
CheckNeighborsAndDestory(hit.transform);
}
if (Rayast up gets a hit) {
CheckNeightorsAndDestory(hit.transform);
}
if (Raycast down gets a hit) {
CheckNeightborsAndDestory(hit.transform);
}
Destory(target.gameObject);
}