- Home /
Updating new cube face positions after rotation
Hi,
I have a game object with a box collider that has a child with a mesh. I rotate the mesh at 90 degree intervals around the x y & z axis but keep the parent objects rotation locked. This makes up a building block which is stored as a prefab. There are multiple types of this prefab but they all follow this format, mainly just the mesh changes.
I connect blocks to each other (all building blocks become children of a single parent) however some block types cant accept another block on a certain face. I have a script "ConnectorControllerScript" attached to each building block that stores an array of 6 booleans that correspond to the Up, -Up, Forward, -Forward, Right and -Right directions.
To place new blocks, I have another script which shows a transparent building block in the place of where the new one is going to go. This allows you to rotate it around the x,y,z axis before placing it. When the block is instantiated I set the rotation of the mesh to a quaternian.identity as for some reason meshes were being instantiated with rotation applied despite the mesh child having zero rotation on any axis in the prefab. I then rotate the mesh to the same position as the transparent building block.
So far so good, thanks for reading this far...
Now after the block is instantiated I need to rotate the positions the ControllerConnectorScript array represents to match the new building block orientation.
To achieve this I check the meshes up direction and compare that to the 6 world directions (i.e. Vector3.up, -Vector3.up, Vector3.right etc) Depending upon which one it hits, it returns a an index in an array corresponding to the new cube faces position. I repeat this process for each mesh directions and swap out the positions as shown in the code.
At least that's what it should do. It kinda works and the array is updated but the updated positions being in the right position have a success rate of about 50% and I've been hitting my head against this one for the last 3 nights.
I've attached the code that does the checking. I've tried a few other methods but I just can't seem to crack this one.
Thanks for taking the time to look at this one!
void updateFaceConnections(GameObject mesh){
bool[] tempBool = new bool[6];
for (int i = 0; i < 6; i++)
tempBool[i] = connections[i];
connections[NewFaceIndex(mesh.transform.up)] = tempBool[0];
connections[NewFaceIndex(-mesh.transform.up)] = tempBool[1];
connections[NewFaceIndex(mesh.transform.forward)] = tempBool[2];
connections[NewFaceIndex(-mesh.transform.forward)] = tempBool[3];
connections[NewFaceIndex(mesh.transform.right)] = tempBool[4];
connections[NewFaceIndex(-mesh.transform.right)] = tempBool[5];
}
int NewFaceIndex(Vector3 direction){
if (direction == Vector3.up)
return 0;
else if (direction == -Vector3.up)
return 1;
else if (direction == Vector3.forward)
return 2;
else if (direction == -Vector3.forward)
return 3;
else if (direction == Vector3.right)
return 4;
else if (direction == -Vector3.right)
return 5;
else
return -1;
}
Just the wrong faces unfortunately. The -1 is there to force an exception error so I know if some really bad happens.
Your answer
Follow this Question
Related Questions
transform.rotation problem 1 Answer
Problem with the look direction after Instanciate/Rotate 0 Answers
Rotate Around Object 2 Answers
Rotating a transform using it's child 2 Answers