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 HeavensVibe · Feb 25, 2013 at 02:51 PM · levelpuzzleautomaticcomplete

Puzzle Box Level Complete - Help Needed!

I am developing a small puzzle game where the user needs to create the image on a 4x4 puzzle box within a time limit. I have so far coded it so once the timer hits 0 the game over screen will appear but I need a way to ccomplete the level once the image is created.

I will use screenshots to show you what I have.

Current Game Screen when run: http://gyazo.com/e9f86b98c523f449238660255b4c7eb2

I have 16 tiles to hold all 15 images, 1 is left blank to allow for movement. http://gyazo.com/e5db02d98da940cd7d1b0a5a23b97552

Can anyone help me code a win sequence once the tiles reach the correct layout as shown in the 2nd print screen. I will be very gratefull as this is due in in a few weeks.

Comment
Add comment · Show 3
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 robertbu · Feb 25, 2013 at 04:48 PM 0
Share

How to check for a winning position heavily depends on your code. How you do your layout? How and where do you represent positions of the tiles? How do you move the tiles? Post some code or give a good description and likely someone can point you in the right direction.

avatar image HeavensVibe · Feb 25, 2013 at 05:02 PM 0
Share

This is the code for the bricks actual motion in javascript form.

 var emptyslot : Transform;
 var xtemp;
 var ytemp;
 var slide : AudioClip;
 var complete;
 
 function On$$anonymous$$ouseUp()
 {
     if (Vector3.Distance(transform.position,emptyslot.position)==1)
     {
         xtemp = transform.position.x;
         ytemp = transform.position.y;
         transform.position.x=emptyslot.position.x;
         transform.position.y=emptyslot.position.y;
         emptyslot.position.x=xtemp;
         emptyslot.position.y=ytemp;
     
         audio.PlayOneShot(slide);
     }
 
 }
 
avatar image Himani_123 · May 16, 2014 at 12:42 PM 0
Share

@ HeavensVibe i am the beginner and im also trying to make the slider puzzle game..,i just want to know how did you shuffle the cubes in the puzzle..?? to move i also used the same code please help me

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Meater6 · Feb 25, 2013 at 02:56 PM

I suggest having a "correct" tile position for each of the 15 images. Simply check if they are all in the right place. Check every time a block moves instead of every frame to be more efficient.

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

Answer by robertbu · Feb 25, 2013 at 05:38 PM

Given how you are handling your movement, I'd create a script that is attached to each tile object. In that script I'd store the winning position for that tile. If you know the positions where you are going to place the tiles, you can just specify the position in the inspector. Or you can specify a Transform variable and drag and drop the tile objects onto the variables (you'll need an empty game object in the open slot). So if a tile is current at (2,1) and its winning position is (0,2), then you drag the game object at (0,2) onto the transform variable. Then you can have a method to check if the Tile is at is home position. Something like (untested):

 public var transHome : Transform;
 private var v3HomePos : Vector3;
 
 function Start () {
     v3HomePos = transHome.position;
 }
 
 function IsHome() {
     return (transform.position - v3HomePos).magnitude < 0.01; 
 }

Say this was the "Tile" component. If you had a script at the root object of all the tiles you could do something like:

 private var tiles : Component[];
 
 function Start () {
     tiles = GetComponentsInChildren (Tile);
 }
 
 function HasWon() :  boolean {
     for (var tile : Tile in tiles) {
       if (!tile.IsHome())
           return false;
      }
      return true;
 }
Comment
Add comment · Show 19 · 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 HeavensVibe · Feb 25, 2013 at 07:25 PM 0
Share

Im not fully sure how I should execute this. This is what I have so far.

I have created a new script called "Tile" which contains this piece of code:

 public var transHome : Transform;
 private var v3HomePos : Vector3;
  
 function Start () {
     v3HomePos = transHome.position;
 }
  
 function IsHome() {
     return (transform.position - v3HomePos).magnitude < 0.01; 
 }

I then applied this to my 8 tiles (changed from 4x4 to 3x3 for ease) leaving the empty slot without anything. Using the inspector I assigned each tile its correct position so for example the print screen below shows Tile R1-2, but the correct placement should be Tile R3-3 which I have allocated in the inspector.

http://gyazo.com/700117226a9b735834e914ae7af45f5d

Is this correct? What should I do next to make the level complete once all the correct positions have been filled? What do I use the 2nd piece of code for? and where/which script to i add this too?

avatar image robertbu · Feb 25, 2013 at 07:44 PM 0
Share

What you did looks right. Note you'll need an empty game object in the open slot, so you have a position for the piece that will get assigned to that slot.

The game is solved when all the pieces are in the home positions. The code above has each piece checking its own home position. To check if they are all in their home positions, you need to query each piece to see if it at its home position. There are a number of ways of getting and checking all the pieces. The code above is a suggestion and would be attached to a game object that is the parent of all the tiles. Anytime you wanted to know if the game was solved, you would call HasWon(). If it returns true, then the game is won.

avatar image HeavensVibe · Feb 25, 2013 at 09:08 PM 0
Share

I have created a javascript called HasWon and used the code you provided. I have allocated a home for the emptyslot which is already its start position to begin wiith. I am however confused as to what to do next.

So far I have an empty object holding all of my tiles in my hierarchy labled "TILES" is this where I would apply the HasWon script if not then where? http://gyazo.com/029c370557321bf957e5d8fa4f345357

From the look of this script it doesnt seem to be automated, im wanting the puzzle to automatically move onto the next level once the image is correctly matched and i cannot get it to do that.

avatar image robertbu · Feb 25, 2013 at 09:51 PM 0
Share

Yes the scrip is attached to the empty game object which is the parent of all the tiles. I don't know anything about how to move on to the next level, but a simple solution would be to check HasWon() either every frame, or at a regular frequency:

 function Update()
 {
    if (HasWon())
        //Code to go to the next level; 
 }

It would be a bit more complicated to have it only check when a piece moves.

avatar image HeavensVibe · Feb 25, 2013 at 10:35 PM 0
Share

I think i've confused myself even further and made it worse :S

 private var tiles : Component[];
  
 function Start () 
 {
     tiles = GetComponentsInChildren (Tile);
 }
  
 function HasWon() :  boolean 
 {
     for (var tile : Tile in tiles) 
      {
       if (!tile.IsHome)
         return false;
      }
      return true;
 }
 
 function Update()
 {
     if (HasWon == true)
     {
         NextLevel();
     }
 }
 
 function NextLevel()
 {
      Application.LoadLevel("Level 2");
 }

So far I have this, but the game will not move onto "Level 2" once the image is correct, I assume my update function is incorrect but I have no idea how set anything beyond what I currently have :S

Show more comments

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

11 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

Related Questions

Activating Completed Level Panel using if statement 0 Answers

Complete Level Script 1 Answer

Detect the previous level? 2 Answers

Video showing in the new level 0 Answers

How to set up a task that after completed sends you to next level? 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