3D Puzzle Generator
What is this
This is a 3D Puzzle Generator to, well, create 3D puzzles of any size.
Here is an example of a 3x3 puzzle:
There are one Start piece, one End piece, Cross Shaped, T Shaped, L Shaped and Line
Shaped pieces.
How To Use
Setup
In the Prefabs folder, you will find a prefab called Puzzle which you can
drop to the scene. This is all you need fo the setup.
Generate a Puzzle
There are 2 scripts attached to the prefab. One is PuzzleGenerator which
you can use to generate the puzzle
Width and Height are the width and height of the puzzle, respectively.
Dist Between Pieces is a distance between neighbouring pieces. Puzzle
will probably not work if the distance is large.
Start Piece is the piece that starts the puzzle, End Piece is the end piece.
You need to connect the Start and the End to complete the puzzle.
Puzzle Pieces are the pieces that you use to connect the Start and the
End.
Generate Puzzle generates the puzzle, but first checks if there is already a
puzzle generated and if so, deletes it.
Delete Puzzle just deletes the created puzzle if any.
As you can see, you can use your own prefab for the puzzle piece.
The second script component is Puzzle Master.
Puzzle Master is basically what controls the puzzle and checks if it is
completed or not.
Rotate Pieces After Completed lets you rotate the pieces even when the
puzzle is completed. Just in case you want that.
Completed gets checked when the Puzzle is completed.
Default Pipe Colour : default colour for the “pipes”, the connection lines
when they are not connected.
Default Piece Colour : colour of the puzzle piece.
Connected Pipe Colour : the colour the “pipes” get when they are
connected.
Selected Piece Colour : when the mouse is hovering the piece, the piece
gets this colour.
On Puzzle Complete event is called when the puzzle is completed. You
can put anything there you want to happen upon completing the puzzle.
Replace a Puzzle Piece
If you do not like the created puzzle and want to easily replace a piece, there is a super easy
way to do this:
1) Select the piece you want to replace.
2) On the gameobject, you will find this script component:
Here you choose by which piece you want to replace the selected piece. And press
Replace. The old piece will be replaced with a new one.
Puzzle Piece
Each puzzle piece has 2 script components attached.
One is Puzzle Piece Replace that I already discussed. The other is PuzzlePiece
Variable Puzzle Piece is a reference to a scriptable puzzle object that contains the information
about that piece. See about the scriptabe below.
On Click event is the event called when we click the puzzle to rotate it.
Puzzle Connect
Puzzle connect is what connects one piece to the other. It is a child of a Puzzle Piece
gameobject.
The box colliders are the connectors.
They have a script attached to them:
If the connector is a transmitter or a receiver, the corresponding box is checked.
Transmitter And Receiver
Puzzle connects can be either Transmitters or Receivers. Initially there is only one Transmitter
and that is the Start piece connector.
When another piece is connected to the Start piece, the connector that is connected to the start
connector (transmitter), becomes a receiver. And the other connectors of this piece become
transmitters. The same happens when another piece is connected to either of these new
transmitters.
If a piece loses the connection with the transmitter, then it loses its receiver and its own
transmitters.
Scriptable Object
There is a scriptable object for the puzzle pieces.
Axis to Rotate : the axis on which the puzzle rotates. Normally, we want that to be Y.
Rot Degrees : how much degrees should a piece rotate on one rotation. Normally we want it to
be 90.
Rot TIme : Time that is spent on the rotation.
Vertical Move Time : Time that is spent on moving the piece up.
Rot Wait Time : the time the piece waits for the rotation to be finished so it can go vertically
down to the original Y position.
Move Down Wait Time : the time the piece waits for the moving down so it can rotated again.
Dist Up : distance the piece goes up on the Y axis.
SFX : audio clips played when moving up, rotating, moving down.