Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Puzzle Help Needed

Discussion in '2D' started by Damocles-256, Jul 10, 2015.

  1. Damocles-256

    Damocles-256

    Joined:
    Jul 10, 2015
    Posts:
    2
    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!
     
    Last edited: Jul 10, 2015
  2. Sickwitit

    Sickwitit

    Joined:
    Dec 22, 2014
    Posts:
    123
    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
     
    BusyCat likes this.
  3. Damocles-256

    Damocles-256

    Joined:
    Jul 10, 2015
    Posts:
    2
    I know how to use the mouse that is not an issue, the issue is how to make the snap to grid rules.
     
  4. Sickwitit

    Sickwitit

    Joined:
    Dec 22, 2014
    Posts:
    123
    @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.