Good day to all. I decided to write a tag on Unity3D, but got stuck and depressed. I wanted to implement random mixing of the pyatnaks, but I can’t understand where I’m wrong, so help out.

So I plan to have them look at the beginning: enter image description here

And this is how I want to mix them:

enter image description here

The result of all my attempts to implement is always like this:

enter image description here

It turns out that part of the chips always climbs on each other.

public class Primer : MonoBehaviour { private GameObject[] plastinka; private GameObject[] newObject; private Transform[] plastinkatransform; void Start () { newObject = GameObject.FindGameObjectsWithTag("Plastinka"); plastinka = GameObject.FindGameObjectsWithTag("Plastinka"); plastinkatransform = new Transform[16]; for (int i = 0; i < plastinka.Length; i++) { plastinkatransform[i] = plastinka[i].transform; } Shuffle(plastinkatransform); // var random = new System.Random(DateTime.Now.Millisecond); // plastinka = plastinka.OrderBy(x => random.Next()).ToArray(); for (int i = 0; i < plastinka.Length; i++) { newObject[i].transform.position = plastinkatransform[i].position;} void Shuffle(Transform[] deck) { for (int i = 0; i < deck.Length; i++) { Transform temp = deck[i]; int randomIndex = UnityEngine.Random.Range(0, deck.Length); deck[i] = deck[randomIndex]; deck[randomIndex] = temp; } } 
  • 1. Why do you mix objects at once? Maybe I don’t understand something in a unit, but it seems to me to mix a simple two-dimensional array of numbers and, on its basis, to arrange objects somewhat easier. 2. Do you know that if you just randomly arrange the chips - the puzzle may not have a solution? Just rearrange the positions 14 and 15 (leave the rest in the initial position in order) and try to assemble. You need to mix the tags in the same way as you collect them, the element of randomness may be in the choice of one of 2-4 chips to shift, depending on the position of the free cell. - rdorn
  • Of course, there is also a workaround - there is a purely mathematical criterion for the availability of a solution, you can take a completely random filling and, after checking for the presence of a solution, make an adjustment. It may or may not be easier, depending on your knowledge. - rdorn
  • I created 2 arrays, I mix 1 array, because while mixing, its transform.position changes. Since in the second array there is an initial transform.position, I take its game object and ask it a new position vector from the intermixed array, in theory I must fully arrange it. But the result is always the same. - LexaMV
  • With mixing, everything seems to be ok, well, except for the lack of guarantees for a solution. I don’t like something in the transfer of positions, but my knowledge ends here and the unit begins = (Do I have to touch it with my own hands ... - rdorn
  • one
    And why do you even touch objects of a unit, mix it up in code. Otherwise, as I understand it, you are trying to mix the objects themselves, which on the stage you visualize an array. Mix in the code, you can even start with a fully assembled array and step by step, following a specific algorithm, interfere with the elements of the array, followed by animation on the stage. - Tobias

1 answer 1

Thank you all for the comments, in the end it turned out that you need to mix an array consisting of Vector3. I can see that I do not fully understand how GameObject.transform works.