- Home /
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612090405im_/https://answers.unity.com/themes/thub/images/avi.jpg)