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 HBishop · Apr 27, 2019 at 04:14 PM · raycastlistraycastall

Tower of Hanoi style

Hi, I'm new to unity and I don't know the best way to store data about a game with similar mechanics to Tower of Hanoi. I have 3 empty game objects that act as the poles and 2 red blocks and 2 blue blocks (disks). How do I use Raycasts to store which blocks are where (They are all on a pole) at initialization and after each move. or is there a simpler way of achieving this. Thank you!

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
1

Answer by JDelekto · Apr 27, 2019 at 04:51 PM

I would probably create a couple of scripts. One script would hold the properties of the disc such as the color and its size for order checking (you could call the script 'Disc', for example). The second script I would apply to the poles and they would be a collection (array or list) of game objects which have the Disc script components.

If you go that route, no ray casting required, you just initialize each pole's collection with the disc game objects in your Start() method. When the user moves one disc from a pole to another, you'll have to get the properties of the disc being moved, check them with the top item in the collection on the pole to where it is being moved, then see if it can be moved. If it can, remove it from the collection on the originating pole and add it to what would be the "top" of the collection on the destination pole.

The mechanics of manipulating the discs can be however you like, but that pattern should be sufficient for a Tower of Hanoi simulation.

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 HBishop · Apr 27, 2019 at 05:13 PM 0
Share

Thanks for the fast reply. Is it at all possible to see some pseudo code (similar). $$anonymous$$g. I'm not sure how the pole script would "communicate" with the disk script.

avatar image JDelekto HBishop · Apr 27, 2019 at 05:46 PM 1
Share

For a simple starter (Note, we will be working with empty game objects, you can render them as you see fit):

1) Create an empty game object, call it "Poles". 2) Create three empty game objects, call them "Pole 1", "Pole 2", "Pole 3", make them all direct children of "Poles". 3) Create an empty game object, call it "Discs". 4) Create two empty game objects, call them "Red Discs" and "Blue Discs", make them direct children of "Discs". 5) Create three empty game objects, call them "Red 1", "Red 2", "Red 3", make them direct children of "Red Discs". 6) Create three empty game objects, calle them "Blue 1", "Blue 2", "Blue 3", make them direct children of "Blue Discs".

Now, let's write the scripts.

7) Create the "PoleScript" script which will hold the disc game objects on that pole: 8) Create the "DiscScript" script which will hold the attributes of the disc, specifically their size and color:

Now let's apply the scripts to the objects:

9) Select Pole 1, Pole 2, Pole 3 and then drag and drop the "PoleScript" onto them in the Inspector. 10) Select Red 1, Red 2, Red 3, Blue 1, Blue 2, Blue 3 and then drag and drop the "DiscScript" onto them in the Inspector.

Now, let's color the discs:

11) Select Red 1, Red 2 and Red 3 and then change their color in the Disc Script to Red. 12) Select Blue 1, Blue 2 and Blue 3 and then change their color in the Disc Script to Blue.

Great, now let's set sizes (weights, diameters, measurements, etc.)

13) Select Red 1 and Blue 1 and type "1" for size property in the Disc script. 14) Select Red 2 and Blue 2 and type "2" for size property in the Disc script. 15) Select Red 3 and Blue 3 and type "3" for size property in the Disc script.

Now, the fun part, putting the discs on the poles:

Select Pole 1, drag and drop the Discs in the order you would like them to start initially onto the "Discs" property on the Pole Script, repeat the process for Poles 2 and 3. $$anonymous$$ake sure you set up the puzzle correctly.

I will leave the rest of the exercise up to you as to how you will the Disc game objects between the "Discs" collections on the Pole Objects script.

The Scripts:

 // PoleScript.cs
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PoleScript : $$anonymous$$onoBehaviour
 {
     public List<GameObject> discs;
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DiscScript : $$anonymous$$onoBehaviour
 {
     public int size;
     public Color color;
 }
 

Now, when it comes to getting access to script data attached to the GameObjects, just use poleObject.GetComponent() and discObject.GetComponent(). The Pole objects have a collection of Disc game objects, so it can iterate through them and manipulate the collection as it sees fit.

avatar image HBishop JDelekto · Apr 27, 2019 at 08:23 PM 0
Share

Thank so much, The set up is working fine and I've learnt a lot. But the scripts to interact and me trying to GetComponent aren't working but I'll keep trying. Thanks so much for all your help! You're a really good $$anonymous$$cher.

avatar image HBishop · Sep 21, 2019 at 08:55 AM 0
Share

Where would the code about add and remove disc be? In pole script or disc script or a disc manager code that uses GetComponent to reference them? How would I reference the poles as they are game object ? I.e. add disc to the middle pole? How would I get it to go into that poles array? Thanks

avatar image Bunny83 HBishop · Sep 21, 2019 at 01:41 PM 0
Share

If you're just looking for an example implementation of the towers of hanoi, have a look at my example. The whole game is just a single C# script with about 216 lines of code. It's just a pretty straight forward implementation without much setup required in Unity. All you need is to setup the 3 poles, reference them from the script and provide two AudioClips which should play when you pick up / drop a disc.


The game itself is controlled by the keys 1,2 and 3 for the corresponding pole.

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

154 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

Related Questions

A node in a childnode? 1 Answer

List of objects in line of sight - most optimized way to write it? 0 Answers

Help with casting and drawing multiple raycast along an angle 0 Answers

Moving a gameobject to another location when tapped - Raycasting and Lerp,Want to lift a gameobject when it is hit - Raycasting and Lerp 0 Answers

graphic raycast with a certain direction ? 0 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