Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hdtvee · Apr 30, 2018 at 04:23 PM · unity 5store

Create a hanoi tower game.

Hello everyone, I am quite new to Unity and C# and want some help if anyone knows. I am trying to create a simple "Tower of Hanoi" game and I don't know how to parameterize and store the available targets a ring can move to.

For example if a ring is on pole A, available targets should be B or C or if it is on pole B, available targets should be on pole A or C. I am moving the ring with void OnMouseDrag() function and thinking that the right place to update the ring position and available targets is on void MouseUp() function.

Any help would greatly appreciated.

Comment
Add comment · Show 2
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 AmirSavand · Apr 30, 2018 at 05:00 PM 0
Share

What is your problem or question? It is unclear therefore your question is invalid.

avatar image hdtvee AmirSavand · Apr 30, 2018 at 05:16 PM 0
Share

Sorry for being unclear. I am asking how to store to the ring object the available targets/poles that can move to and when the movement is done, update the new target poles.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by the_genius · Apr 30, 2018 at 06:14 PM

You'll need a script to keep track of which Rings are on which poles:

 public class RingPositions : MonoBehaviour
 {
     public static List<Ring>[] poles = new List<Ring>[3] { new List<Ring>(), new List<Ring>(), new List<Ring>() };
 }



And a script attached to each ring. Something like this:

 public class Ring : MonoBehaviour
 {
     public int size = 0; // The size of the ring. Larger value = bigger ring
     public int poleNumber = 1; // The pole number this ring is on. Either 1, 2 or 3
 
     bool isBeingDragged = false;
 
     void Start ()
     {
         RingPositions.poles[poleNumber].Add(this as Ring);
     }
 
     void OnMouseDown ()
     {
         // Find the minimum ring size on this pole
         int minSize = 99999;
         foreach (Ring r in RingPositions.poles[poleNumber])
         {
             if (r.size < minSize)
             {
                 minSize = r.size;
             }
         }
 
         // Can only move the ring if it is the minimum size ring on the pole
         if (size == minSize)
         {
             isBeingDragged = true;
         }
     }
 
     void OnMouseDrag ()
     {
         if (isBeingDragged)
         {
             // Update position of this object to follow the mouse
         }
     }
 
     void OnMouseUp ()
     {
         // Find the position of the nearest pole
         int nearestPoleNumber = ..... ; // Either 1, 2 or 3
 
         // Make sure we have dragged it to different pole
         if (poleNumber != nearestPoleNumber)
         {
             // Find the smallest ring on the new pole
             int minSize = 99999;
             foreach (Ring r in RingPositions.poles[nearestPoleNumber])
             {
                 if (r.size < minSize)
                 {
                     minSize = r.size;
                 }
             }
 
             // This ring must be smaller if we can put it on this pole
             if (size < minSize)
             {
                 RingPositions.poles[poleNumber].Remove(this as Ring);
                 RingPositions.poles[nearestPoleNumber].Add(this as Ring);
                 // Change the position to the new pole
             } else
             {
                 // Change position back to the pole it was on
             }
         } else
         {
             // Change position back to the pole it was on
         }
         isBeingDragged = false;
     }
 }

This keeps track of the size of each ring and doesn't allow for placing large rings onto smaller ones. You'll have to fill in the gaps, updating the rings positions when being dragged or moved to the new ring or being moved back to the old ring.
You also need a way of finding which pole is nearest when you drop the ring. I suggest using a gameObject for each pole and Vector3.Distance to find the nearest.

Comment
Add comment · Show 2 · 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 hdtvee · Apr 30, 2018 at 07:59 PM 0
Share

Thank you for your input, but your solution does not address my problem.

avatar image the_genius · Apr 30, 2018 at 08:45 PM 0
Share

Please could be clearer on your problem. I believe my answer clearly explains a method of "how to store to the ring object the available targets/poles that can move to and when the movement is done, update the new target pole"

avatar image
0

Answer by tacman1123 · Sep 16, 2020 at 07:58 PM

A Unity version of this game can be found at:

https://juanborgesv.itch.io/tower-of-hanoi

Source code is also available at https://github.com/juanborgesv/TowerOfHanoi

Comment
Add comment · 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

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

166 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to store 3d object with all scripts? 1 Answer

I Get Error when i purchase from unity store 2 Answers

You need to use a different version code for your APK or Android App Bundle because you already have one with version code 200002. 0 Answers

No unity asset store Package Manager Button on unity 5 0 Answers

Asset Store not loading when i click on download 2 Answers


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