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 tonyjoseph456 · Aug 08, 2016 at 01:07 PM · drag-and-droppuzzledrag objects

Jigsaw Puzzle Drag & Drop issues.

Hi,

In my game, of jigsaw of 3*3 tiles. I need to drag the jigsaw tile and drop into the correct position. The problem is my drag and drop is not perfect. If I drag a jigsaw tile and while dragging, I release my finger and tap and hold the piece again, the position of the jigsaw tile is being changed and its not reverted to the start position. So the jigsaw tile stays in the mixed with other jigsaw tiles. It is not going back to the previous start position. I guess I have done a simple mistake somewhere in the script and I cant find it where is the mistake. Since I'm relatively new to C#, I can't figure out what the error is. I'm using itween for moving the pieces. Can some one please help me. Thanks in advance.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 using System;
 
 public class JigsawDragController : MonoBehaviour{
     Vector3 GUIWorldPoint;
     Vector3 offset;
     Vector3 curScreenSpace,curPosition,temp,endPosition,startPosition;
     public GamePlay GameScript;
     GameObject[] Jigsaw;
     public float mindistance;
 
   
     void OnMouseDown()
     {
        
         if (GameScript.isMoving)
         {
             Debug.Log("Already moving OnMouseDown Function "+System.DateTime.Now);
             return;
         }
            
         GameScript.isDragging = true;
         startPosition = this.gameObject.transform.position;
         Debug.Log("Start Position: " + startPosition+" "+this.gameObject.name );
         Jigsaw = GameScript.JigsawArray;
         for (int i = 0; i < Jigsaw.Length; i++)
         {
             Jigsaw[i].gameObject.GetComponent<SpriteRenderer>().sortingOrder = 1;
             
         }
         this.gameObject.GetComponent<SpriteRenderer>().sortingOrder = 50;
         this.gameObject.GetComponent<SpriteRenderer>().sortingLayerName = "UI overlay";
         Debug.Log(DateTime.Now);
 
        GameScript.SelectedJigsaw1 = this.gameObject;
         GamePlay.IsJigsaw1Selected = true;
         GUIWorldPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
         GameScript.SendMessage("StoreJigsawTempPosition");
         offset = this.transform.localPosition - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, GUIWorldPoint.z));
     }
 
     void OnMouseDrag()
     {
         if (GameScript.isMoving)
         {
             Debug.Log("Already moving OnMouseDrag Function " + System.DateTime.Now);
             return;
         }
         this.gameObject.GetComponent<SpriteRenderer>().sortingOrder = 50;
         this.gameObject.GetComponent<SpriteRenderer>().sortingLayerName = "UI overlay";
         curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, GUIWorldPoint.z);
         //convert the screen mouse position to world point and adjust with offset
         curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
         temp = this.transform.position;
         temp.x = curPosition.x;
         temp.y = curPosition.y;
         this.transform.position = temp;
     }
 
     //public void OnEndDrag(PointerEventData eventData)
     void OnMouseUp()
     {
         if (GameScript.isMoving)
             return;
         if (GameScript.isDragging==false)
             return;
         GameScript.isDragging = false;
         endPosition = this.transform.position;
         Jigsaw = GameScript.JigsawArray;
         Bounds JigsawBounds;
         for(int i=0;i<Jigsaw.Length;i++)
         {
             if(Jigsaw[i].gameObject.name!=this.gameObject.name)
             {
                 float dist = Vector2.Distance(this.gameObject.transform.position, Jigsaw[i].gameObject.transform.position);
                 if(dist<mindistance)
                 {
                     
                     //The Jigsaw tile is dragged and positioned to the correct place, So now can flip the pieces.
                     GamePlay.IsJigsaw2Selected = true;
                     GameScript.SelectedJigsaw2 = Jigsaw[i].gameObject;
                     GameScript.FlipPiece();
                     return;
                     
                 }
                 else
                 {
                     //The Jigsaw tile is dragged and positioned to the incorrect place. So now need to move back to the previous starting position.

                     GameScript.isMoving = true;
                     iTween.MoveTo(this.gameObject, iTween.Hash("x", startPosition.x, "y", startPosition.y, "time", 0.5f,"oncomplete","Complete1","oncompletetarget",this.gameObject));
                     GameScript.SelectedJigsaw1 = null;
                     GamePlay.IsJigsaw1Selected = false;
                     GamePlay.IsJigsaw2Selected = false;
                     GameScript.SelectedJigsaw2 = null;
                 }
             }
         }
     }
     void Complete1()
     {
         GameScript.isDragging = false;
         GameScript.isMoving = false;
         for (int i = 0; i < Jigsaw.Length; i++)
         {
             Jigsaw[i].gameObject.GetComponent<SpriteRenderer>().sortingOrder = 1;
             Jigsaw[i].gameObject.GetComponent<SpriteRenderer>().sortingLayerName = "Default";
         }
     }
     // Use this for initialization
     void Start () {
         Jigsaw = GameScript.JigsawArray;
     }
 
     // Update is called once per frame
     void Update () {
     
     }
 }
 GamePlay Script
 
 public void FlipPiece()
     {
         movesTaken++;
         for (int m2= 0; m2 < JigsawArray.Length; m2++)
         {
             if (SelectedJigsaw2.gameObject.name == JigsawArray[m2].gameObject.name)
             {
                 Temp2 = JigsawArray[m2];
                 Index2 = m2;
             }
         }
         JigsawArray[Index1].SendMessage("ChangePosition", Index2);
         JigsawArray[Index2].SendMessage("ChangePosition", Index1);
         Debug.Log("Index 1: " + Index1 + " Index 2: " + Index2);
         Temp1 = JigsawArray[Index2];
         JigsawArray[Index2] = JigsawArray[Index1];
         JigsawArray[Index1] = Temp1;
         GamePlay.IsJigsaw1Selected = false;
         GamePlay.IsJigsaw2Selected = false;
         SelectedJigsaw1 = null;
         SelectedJigsaw2 = null;
         ResultJigsaw = Check();
         Debug.Log("Result " + ResultJigsaw+" "+System.DateTime.Now);
         if (ResultJigsaw == true)
         {
 //The jigsaw pieces are all arranged correctly. Game Win.
          }
  }
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

0 Replies

· Add your reply
  • Sort: 

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

50 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

Related Questions

Drag and Drop "Puzzle" Game con't always work 0 Answers

Drag and drop picks an object too far away from mouse position 2 Answers

spawn and drag game object without leaving mouse button 1 Answer

Check to see if item is in correct slot 1 Answer

Move / Place objects on plane in 3D space 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