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 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
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

129 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

Related Questions

Jump by touch 1 Answer

I have to do if the touch slider to the left, right, and the character will move to the left, right under the sensors for a certain location. 0 Answers

How to exclude specific touch area! 4 Answers

iOS TouchPhase.Began Detected Multiple Times 1 Answer

Short question: If my app runs without any problems on Android, should it run without any problems on iOS? 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