- Home /
Question by
studio43 · Nov 24, 2021 at 05:43 PM ·
touchphase
android export-strange behavior
Hello people.
I made a jigsaw puzzle game, that works ok in editor (and in unity remote). I tested many different resolutions, I also put the mobile simulator asset and test it in different phones (small and big) BUT... when I export it for android it has a very strange behaviour.
The pieces go and stuck in completely random places, and with completely random (as I think) order!
Here is a video I made to show it: https://youtu.be/mm-p8R_Xxus
And below is my code.
Any help would be appreciated.
using UnityEngine;
public class PuzzleNekNEW : MonoBehaviour
{
private int order = 0;
private short i;
private int pieceName;
private float dist;
private bool dragging = false;
private bool check = false;
private Vector3 offset;
private Transform toDrag;
private GameObject[] piecesObjectHierarchy = new GameObject[24];
private Vector3[] startPositions = new Vector3[24];
private Vector3[] startLocalPositions = new Vector3[24];
[SerializeField] private AudioSource audioData;
[SerializeField] private int[] check24 = new int[24]; //24 ints set to 0. When I place correctly a piece,, its position in this table is set to 1
private void Start()
{
//take pieces from hierarchy in an array of objects
piecesObjectHierarchy = GameObject.FindGameObjectsWithTag("pieces");
//record for each object the initial local position in an Vector 3 array
for (i = 0; i < 24; i++)
{
//startPositions[i] = piecesObjectHierarchy[i].transform.position;
startLocalPositions[i] = piecesObjectHierarchy[i].transform.localPosition;
}
//distribute the pieces randomly
for (i = 0; i < 24; i++)
{
piecesObjectHierarchy[i].transform.position = new Vector3(Random.Range(-1.9f, 2.6f), Random.Range(4.0f, -4.5f), 0);
}
//fill the check table with zeros (0)
for (int j = 0; j < 24; j++) check24[j] = 0;
}
private void Update()
{
Vector3 v3;
//if you touch with more than one fingers you cannot move the piece!
if (Input.touchCount != 1)
{
dragging = false;
return;
}
Touch touch = Input.touches[0];
Vector3 poz = touch.position;
//JUST TOUCH
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(poz);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
if (hit.collider.tag == "pieces")
{
toDrag = hit.transform;
print(toDrag);
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = new Vector3(poz.x, poz.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
order++;
//send the object you touched on the top
toDrag.GetComponent<UnityEngine.Rendering.SortingGroup>().sortingOrder = order;
}
}
//MOVING TOUCH
if (dragging && touch.phase == TouchPhase.Moved)
{
v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.position = v3 + offset;
pieceName = int.Parse(toDrag.name);
//print("v3 " + v3 + " toDrag.localPosition " + toDrag.localPosition + "startlocalPositions[0] " + startLocalPositions[0]);
//if I move a piece, its position is 0 in the check table
check24[pieceName] = 0;
//if I am close to the initial position!!! (I use Vector2 to just check x and y)
if (Vector2.Distance(toDrag.localPosition, startLocalPositions[pieceName]) < 50f)
{
print(pieceName + " IS IN!!! " + "startLocalPositions[pieceName] " + startLocalPositions[pieceName] + " toDrag.localPosition " + toDrag.localPosition);
//stop moving this piece
dragging = false;
//it was very close, Give it the exact initial position
toDrag.localPosition = startLocalPositions[pieceName];
//put an 1 in the check table
check24[pieceName] = 1;
//send it back
toDrag.GetComponent<UnityEngine.Rendering.SortingGroup>().sortingOrder = 0;
}
}
//if I leave my finger
if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
{
dragging = false;
}
//CHECK if the puzzle is completed
check = true;
for (int j = 0; j < 24; j++)
{
if (check24[j] == 0) check = false;
}
if (check == true)
{
dragging = false;
print("YOU DID IT!!!");
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613045756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Jump by touch 1 Answer
How to exclude specific touch area! 4 Answers