Hi,
I’m making a Rubik’s Cube type of situation with 3x3x3 cubes. I’ve made some progress, but I’ve run into several questions. At this point I have been able to successfully gather nine cubes and rotate them smoothly 90 degrees, but only if the function that gathers them is called from the Start function. When I try to call it from an input its jumpy and spins farther than 90 degrees. When testing my code on a single cube not requiring the foreach loop it works fine. In the course of trying to figure it out I have come across several things I can’t explain that may be a part of the issue. Either way I’d love to know why those things are happening as well.
The first, when my “endRotation” and “rightPivot” variables are instantiated Unity makes 27 of them, one for every cube, rather than just the 1 I need. Second, when I have tried to apply a counting variable, “i”, to the foreach loop I have found that it only goes up to 9 and then starts over for a total of 243 times. Also, when I have tried to apply a second counting variable, to count every time “i” gets to 9, it never gets higher than 1.
Thanks in advance for any help.
public class ComboRotator : MonoBehaviour
{
private GameObject cubes;
private GameObject rightPivot;
private GameObject endRotation;
private bool gathered;
void Start ()
{
rotateGo = false;
gathered = false;
endRotation = new GameObject ("endRotation");
cubes = GameObject.FindGameObjectsWithTag ("Cube");
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (gathered == false)
{
GatherRight ();
}
endRotation.transform.Rotate (Vector3.right, 90, Space.World);
}
rightPivot.transform.rotation = Quaternion.Lerp (rightPivot.transform.rotation, endRotation.transform.rotation, Time.deltaTime * 8);
}
}
void GatherRight()
{
rightPivot = new GameObject ("rightPivot");
rightPivot.transform.position = new Vector3 (2, 1, 1);
foreach (GameObject cube in cubes)
{
if (cube.transform.position.x == 2)
{
cube.transform.parent = rightPivot.transform;
}
}
gathered = true;
}
}