2D Puzzle Help Needed

I am working on a simple puzzle yet I am totally confused how I should approach the whole thing, because I am new to Unity but I have basic/advanced programming experience.

What I would like to do is:

  • The pieces are moved using the mouse
  • I would like the pieces scattered on the table at start
  • Pieces should snap to the grid but it must not hang out of the grid or the screen
  • No rotation of the pieces
  • The active peace should be top layer
  • Reset button and victory display

The Grid:


The Pieces Scattered:

The Pieces Completed:

Maybe you can point me to a tutorial? Thank you for your time!

I think you should start out with step 1. Find out how to take mouse input from the user and learn how it works. Once you learn how it works, it will make much more sense to you, then you can apply your knowledge. Then once you solve your problem. Move to step 2.

Unity Input scripting reference:
http://docs.unity3d.com/ScriptReference/Input.html

1 Like

I know how to use the mouse that is not an issue, the issue is how to make the snap to grid rules.

@Damocles-256

You will most likely need to create some sort of PuzzleManager class that manages each puzzle peice and their rules. Some good old OOP can be used also, along with polymorphism to separate specific behaviors based on each different puzzle piece.

But, for a simple solution:
The board squares can be separated into individual collision 2d boxes. You could also split the puzzle piece into squares, which individual collision boxes put together. Then, once all collision boxes for the puzzle peice collide with one on the board, all at once using OnCollisionEnter2D, then update the position of the puzzle piece to your desired location. This will create the snap effect.