I'm trying to create a minigame where I have the picture below, sliced into 32 pieces.
On Play/Start all the pieces are rotated randomly in multiplications of 90. From 0 -> [90,180,270].
The goal of the game is to rotate all the pieces to 0 Z-Axis Rotation, then a UI will pop up stating that you win.
The game plays as I expected it to, up to the point of the last 1-3 pieces, where even their rotation is not 0, the UI triggers and the game ends.
Example below, see piece top-right above the UI text.
GameControllerSlice Script
public class GameControllerSlice : MonoBehaviour
{
[SerializeField]
private GameObject[] PicturePieces;
[SerializeField]
private GameObject winText;
[SerializeField]
private GameObject winTextBackground;
public static bool youWin = false;
private readonly int[] randomZ = new int[] {90, 180, 270};
private int randomIndex;
void Start()
{
Randomizer();
winText.SetActive(false);
winTextBackground.SetActive(false);
}
void Update()
{
RotationCheck();
}
//Randomizes the rotation of the Z-axis value.
private void Randomizer()
{
for (int i = 0; i < 4; i++)
{
randomIndex = Random.Range(1, randomZ.Length - 1);
PicturePieces[i].transform.Rotate(0, 0, randomZ[randomIndex]);
}
}
//Checks if the rotation of each pieces is 0.
private void RotationCheck()
{
if (Pictures.All(pic => (int) pic.transform.rotation.z == 0f))
{
youWin = true;
winText.SetActive(true);
winTextBackground.SetActive(true);
}
}
}
On each individual piece I have a the script with the simple code below.
TouchRotate Script
public class TouchRotate : MonoBehaviour
{
private void OnMouseDown()
{
if (!GameControllerSlice.youWin)
{
transform.Rotate(0f, 0f, -90);
}
}
}
I've spend the past 3 days doing trial & error, with no success.
Also tried a different approach shown below, but that gave me a whole other problem, because I can't read/update the index properly from TouchRotate back into the GameControllerSlice script.
Excuse some leftover comments I was trying different things.
public class GameControllerSlice : MonoBehaviour
{
[SerializeField]
private GameObject[] PicturePieces;
[SerializeField]
private GameObject winText;
[SerializeField]
private GameObject winTextBackground;
public static bool youWin = false;
public static int[] RandomizedPics = new int[32];
private readonly int[] randomZ = new int[] {0, 90, 180, 270};
private int randomIndex;
void Start()
{
FillRandomList();
Randomizer();
winText.SetActive(false);
winTextBackground.SetActive(false);
}
void Update()
{
RotationCheck();
}
public void FillRandomList()
{
foreach (int i in RandomizedPics)
{
RandomizedPics[i] = randomZ[0];
// Debug.Log("Value " + RandomizedPics[i]);
}
}
//Randomizes the rotation of the Z-axis value.
private void Randomizer()
{
for (int i = 0; i < 4; i++)
{
randomIndex = Random.Range(1, randomZ.Length - 1);
PicturePieces[i].transform.Rotate(0, 0, randomZ[randomIndex]);
RandomizedPics[i] = randomIndex;
}
}
//Checks if the rotation of each pieces is 0.
private void RotationCheck()
{
for (int i =0; i < 32; i++)
{
if (RandomizedPics[i] != 0)
{
Debug.Log("Picture " + PicturePieces[i] + "value " + RandomizedPics[i]);
}
}
}
}
public class TouchRotate : MonoBehaviour
{
private void OnMouseDown()
{
//if (!GameControllerSlice.youWin)
transform.Rotate(0f, 0f, -90);
//Debug.Log("Clicked Pic " + name + "with value " + System.Array.IndexOf(gameController.Pictures, this));
// Debug.Log(System.Array.IndexOf(gameController.Picturesgo, this.transform.gameObject));
}
}
Appreciate any help or pointers to the right direction for either approach.