- Home /
Pass command from one object to the next
Working on a project that is built around a 3D pyramid of cubes. When a cube is destroyed the one above it needs to drop down to take it's place.
I was thinking I could just tell the next cube up to move with transform.Translate(x,y,z,blah), however I need to figure out how to send that command to the correct cube.
Anyone have any pointers on how I can go about doing this?
Answer by DirusCanis · Dec 28, 2014 at 10:49 AM
Alright, got my implementation to work. I created a script that is attached to each prefab cube that handles a number of things including randomizing the color/texture on start, assigning a variable based on spawn position, and of course handle the fall code.
public void Fall(){ // Handles moving the cubes down the pyramid by checking to see if the space below is empty
tarPos = ownPos; // used for all cubes
tarPos2 = ownPos; // used for corner cubes only
tarPos3 = ownPos; // used for corner cubes only
tarPos.y -= 1.0f; // We're always looking down 1 level.
tarPos2.y -= 1.0f;
tarPos3.y -= 1.0f;
if (cType == 8) { // Side Cube, Move Straight Down
tarPos.z += 1.0f;
if (checkEmpty (tarPos)) { // Pass pos to checker
if (true) {
transform.Translate (0f, -1.0f, 1.0f, Space.World);
}
}
}else if (cType == 1) {// corner cube, need to check 3 positions
tarPos.x += 1.0f;
tarPos2.x += 1.0f;
tarPos2.z += 1.0f;
tarPos3.z += 1.0f;
if (checkIfPosEmpty (tarPos)) {
if (true) {
transform.Translate (1.0f, -1.0f, 0f, Space.World);
cType = 5;
}
} else if (checkIfPosEmpty (tarPos2)) {
if (true) {
transform.Translate (1.0f, -1.0f, 1.0f, Space.World);
}
} else if (checkIfPosEmpty (tarPos3)) {
if (true) {
transform.Translate (0f, -1.0f, 1.0f, Space.World);
cType = 8;
}
}
}
public bool checkEmpty(Vector3 tarPos) {
GameObject[] cTiles = GameObject.FindGameObjectsWithTag("cubeTile");
foreach(GameObject current in cTiles) {
if(current.transform.position == tarPos) return false;
}
return true;
}
There should be a little tick next to your answer (on left). Select it to let others know the question has been answered to your satisfaction
Answer by Navigatron · Dec 26, 2014 at 10:57 PM
In your situation, with a lot of cubes, I would create two cubes, one directly atop the other. Make both these cubes the children of some empty object, where the top cube is at 0,0,0 and the bottom cube is at 0,-1,0. Remove the renderer from the bottom cube, and set its Collider to isTrigger. Save this as a prefab
You now have a prefab cube that can detect when things under it move. From there, each cube can handle its position independantly, with no need for some master class to have references to every cube.
The above setup and a basic script like this one should work fine.
(C#)
void OnTriggerExit(Collider other)//When a Cube below us has been destroyed, or moves downwards
{
transform.parent.position = other.transform.parent.position;
}
I haven't tested this code, you might have to add Vector3.up or down depending on your results.
Best of Luck, ~Navi
I just re-read my OP, and I did not explain things well at all. The object has to look for an empty spot that is down one unit and out from the center one unit as it's a stepped pyramid. The corner blocks have to check for 3 empty spaces, one of which being the next one down on a diagonal.
I'll have to play with this parent object method and see if it works, if the current attempt of $$anonymous$$e doesn't.
$$anonymous$$y current attempt is to add a script to the preFab cubes that assigns them a type based on their original spawn position, then based on the "type" check the appropriate position to see if it's empty, if it is, then it moves down.
Are you attempting to create some kind of 2D candy crush game but as a pyramid ins$$anonymous$$d of a square where the only squares that can be hit are the ones at the bottom?
Not exactly, it is a match 3 based game, but it is ment to be a party game and the board is a 3D pyramid that you can rotate.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174929im_/https://answers.unity.com/themes/thub/images/avi.jpg)