Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BraveVN · Oct 13, 2014 at 07:58 AM · puzzlematching

How to make a puzzle game

Guys/girls, I'm making a 2D puzzle game for kids which they can drag small pieces (there are many kind of shapes like square, rectangle, triangle, circle, L-shape, half moon...) and drop them to a big shape to fill it up (the shape may be a polygonal).

The question is how can I make the orange piece become auto fit to every edges of square when the shape is near the edges, like the situation when we hover a metal piece close to a magnet and it's sticked.

Further more, I use OnMouseDrag() function to drag and drop objects, it works good on PC with mouse but when I build on mobile device, may be it also works on touch screen ?

Thank you.

Edit: All pieces and shapes are 2D sprites

Figure 2

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Oct 13, 2014 at 08:45 AM

(This is using words from new UI but is not related)

Your canvas (the grey rectangle) should have anchor points, simply some empty game objects (Transform). When the button is released you would check if the orange piece is close enough to any of those anchor points and if so you start a lerp process to the point, or you snap there.

 private Transform[] anchor;
 private Transform target = null;
 void Start()
 {
     anchor = Manager.GetAnchor(); // This returns the array of anchor you defined somewhere else
 }
 void Update()
 {
     if(lerp)
     {
         transform.position = Vector3.MoveTowards(transform.position, target.position, ratio);
         if(transform.position == target.position){lerp = false;}
     }
     // More code
 }
 void OnMouseUp()
 {
      target = GetAnchor();
      if(target != null)   
      {
          lerp = true;
      }
 }
 
 private Transform GetAnchor()
 {
    Transform closest = null;
    float distance = Mathf.Infinity;
    float range = 0.2f;
    foreach(Transform t in anchor)  // iterate all anchors
    {
         float dist = Vector3.Distance(transform.position, t.position);
         if(dist < range && dist < distance) // check if close enough and closer than any other already checked points.
         {
               closest = t;
               distance = dist;
         }
    }
    return closest;
 }





Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BraveVN · Oct 13, 2014 at 09:01 AM 0
Share

Thank you! Great idea! But this is the first time I heard "anchor point", what is this and how to use it. Is it a GUI element or a game object ?

avatar image screenname_taken · Oct 13, 2014 at 09:21 AM 0
Share

It is a concept that is used anywhere that you need to snap something to someplace. It is used in the new UI but fafa isn't talking about that. He just means an object at the place you want your puzzle piece to snap to. To be able to check if it's at the correct place.

avatar image BraveVN · Oct 13, 2014 at 09:30 AM 0
Share

thanks you guys :) but I'm using Unity 4.5.4, so I think I don't have the New UI. Is there anyway to do without download the 4.6 beta version ? And further more, all my pieces and shapes are 2D sprite cut off from a big image

avatar image fafase · Oct 13, 2014 at 03:35 PM 1
Share
  (This is using words from new UI but is not related)

I should have actually not used that. Anchor and canvas in my case are not at all the one from the new UI, they are just words I used because they fit the situation. A canvas is just a zone on which you render, in your case I meant canvas because it would be the word I would use n the real world with a real puzzle. The anchor point is just a point that you use as reference but is not related to the new UI in this case.

So the code above will work with any Unity version, with little tweaks it would work for any engine actually.

So Anchors are just emoty gameObjects you place in the scene to define where the sprite should go.

For instance, if the small orange square goes in the top left corner and the position should be 0.5, 0.5 , 0.0 then you place an empty gameObject at that position.

Then you have a manager with:

 [SerializeField] private Transform[] anchors;
 
 public Transform[] GetAnchors()
 {
     return anchors;
 }

The array is where you will drag all the different anchors (empty game objects) you placed in the scene at position where pieces should go.

The public method allows you to get that array from any other components.

You would probably start your piece script with:

 private Transform [] anchors;
 void Start()
 {
     anchors = FindObjectOfType<$$anonymous$$anager>().GetAnchors;
 }

The rest I already gave you.

avatar image BraveVN · Oct 14, 2014 at 12:53 AM 0
Share

Yes, I got it! Thank you, your instructions are very detail. You're a nice coder :D Thanks

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Colour Matching Game: Search board for matches? 2 Answers

Pattern Detection 1 Answer

How to set different score values to different prefabs of an array? 0 Answers

Puzzle Piece Match Mechanic 1 Answer

Creating a 2D shape matching game? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges