I am trying to make a jigsaw puzzle game and would like to get some guidance from you all on the best way to approach the scripting of fitting the puzzle pieces onto the frame which holds them.
I have got the movement done and i can rotate the pieces as well, the last couple of things i need to know is.
What is the best way make it so only one puzzle piece will fit in each area of the frame and will only fit if the piece is rotated to fit in that spot.
When the piece is in the correct spot i need to make it so i can no longer be moved.
I was trying to use raycast to make the puzzle piece recognize the frame which is working, but what i think might work would be if i could shoot out multiple rays from the edges of the piece and then somehow get the frame to recognize when the piece was in the right location and had the right rotation and then stop the piece from moving.
I don’t know how to pull this off though and i have done a lot of searching and have not found an answer.
Is your puzzle broken into pieces that conform to a grid (so, N rows by M columns) or are they more random in nature? If the former, you could use attributes of the grid along with a known location and orientation of each piece to solve many of your issues. If the latter, it’s definitely more difficult.
I would create a 2 dimensional array to store a reference for each piece and name the pieces based on their position in the array. Pieces will go into the array according to their positions when the puzzle is solved. To see if 2 pieces are oriented correctly, just take the dot product of their forward vectors. If the dot product is close to 1 (say > 0.95) and the pieces are close together, then you connect them. (parenting would make sense here).
So when you drop a piece, (or a collection of pieces) you would check if there is an adjacent piece that needs to be attached to the set. Say if you are dropping piece 2-2, then you would check if pieces 1-2, 2-1, 1-3 or 3-1 are close enough to be connected. You access those from the array.
I ended up grabbing the Puzzle pack, now i just have to figure out how to convert the C# to Unity script so i can add rotation to the pieces and change a few other things too.