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 GamingC3 · Sep 10, 2020 at 09:55 AM · bugunity 2dtouchunityandroid

Possible bug in unity touch phase for android ?

I am pretty new to unity and have been working on my first ever android game. Most of the elements function properly but there is this one little inconvenience which makes playing the game almost impossible. I will try to include all of the important info and explain it as best as I can. Excuse my not-so-good grammar and English as a whole, as it is not my first language.

The name of my game is 'Distance! Please' and it revolves around the idea of maintaining distance from every other game object. The characters are called 'Fluffs' and there is a specific number of fluffs on every level. the characters have a script[RandomPatrol] for random movement on the screen. They also have a DragAndDrop Script which allows the player to drag and change the position of the fluffs.

The problem, however, is that if I drag - release 'Fluff A' and then touch 'Fluff B', Fluff A would randomly teleport over to the position of FluffA resulting in the crash of two game objects hence, a big fat 'Game Over'. This is some kind of glitch, bug, or a hardware issue, I'm not particularly sure about that, as it happens randomly. Sometimes it won't happen and you'll be able to clear the level easily but sometimes it just won't let you advance ahead.

To Further explain my situation I have provided some pics down below and also have provided the two scripts I mentioned above.

  1. Normal Touch and drag on Orange Fluff, executed as expected

  2. After releasing orange I dragged red fluff. Worked as intended

  3. After releasing red fluff I tried to drag blue fluff but the red one just moved over, as you can see by the Trail

DragAndDrop Code

 public class DragAndDrop : MonoBehaviour
 {
     bool moveAllowed;
     Collider2D col;
     int flag;
 
     private GameMaster gm;
 
     private AudioSource audioSource;
 
     public GameObject selectEffect;
     public GameObject deathEffect;
     public GameObject electricDeath;
     public GameObject poisonDeath;
     public GameObject fireDeath;
     public GameObject frostDeath;
 
     void Start()
     {
         gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
         audioSource = GetComponent<AudioSource>();
         col = GetComponent<Collider2D>();
     }
 
     void Update()
     {
         if(Input.touchCount > 0 && !IsPointerOverUIObject())
         {
             Touch touch = Input.GetTouch(0);
             Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
             Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
 
             switch (touch.phase)
             {
                 case TouchPhase.Began:
                     
                     if (col == touchedCollider)
                     {
                         Instantiate(selectEffect, transform.position, Quaternion.identity);
                         audioSource.Play();
                         //moveAllowed = true;
                         flag = 1;
                     }
                     break;
 
                 case TouchPhase.Moved:
                     if (flag == 1)
                     {
                         transform.position = new Vector2(touchPosition.x, touchPosition.y);
                     }
                     break;
 
                 case TouchPhase.Ended:
                     flag = 0;
                     //moveAllowed = false;
                     break;
 
                 case TouchPhase.Canceled:
                     //moveAllowed = false;
                     flag = 0;
                     break;
             }
         }
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "Planet" || collision.tag == "Projectile")
         {
             Instantiate(deathEffect, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
         if (collision.tag == "ElectricOrb")
         {
             Instantiate(electricDeath, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
         if (collision.tag == "Spiky")
         {
             Instantiate(poisonDeath, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
         if (collision.tag == "FireOrb")
         {
             Instantiate(fireDeath, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
         if (collision.tag == "FreezeOrb")
         {
             Instantiate(frostDeath, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
     }






RandomPatrol Code

 public class RandomPatrol : MonoBehaviour
 {
     private float _speed;
 
     public float minSpeed = 1f;
     public float maxSpeed = 2f;
 
     public float minX = -8.15f;
     public float maxX = 8.15f;
     
     public float minY = -4.15f;
     public float maxY = 4.15f;
 
     public float secondsToMaxDifficulty = 50f;
 
     Vector2 targetPosition;
 
     void Start()
     {
         Time.timeScale = 1;
         targetPosition = GetRandomPosition();
     }
 
     void Update()
     {
         if((Vector2)transform.position != targetPosition)
         {
             _speed = Mathf.Lerp(minSpeed, maxSpeed, GetDifficultyPercent());
             transform.position = Vector2.MoveTowards(transform.position, targetPosition, _speed * Time.deltaTime);
         }
         else
         {
             targetPosition = GetRandomPosition(); 
         }
     }
 
     Vector2 GetRandomPosition()
     {
         float randomX = Random.Range(minX, maxX);
         float randomY = Random.Range(minY, maxY);
         return new Vector2(randomX, randomY);
     }
 
     private float GetDifficultyPercent()
     {
         return Mathf.Clamp01(Time.timeSinceLevelLoad / secondsToMaxDifficulty);
     }
 
 }






I have already tried fixing it and looked for a solution for 4 days now. Any kind of help. I have tried playing it on unity remote as well as installed apk, the problem continues to persist.

Any kind of help would be highly appreciated and I'll be more than happy to provide any kind of extra information you may ask/need.

Comment
Add comment · Show 4
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 whogas · Sep 10, 2020 at 07:38 PM 0
Share

Does it only happen when you are near UI objects? Is the UI check interfering with the touch phase change happening?

Is it always that the touch end doesn't register?

Have you turned on the debugging touches display on your android phone? It is in different locations, but somewhere in the dev settings it should allow you to see exactly where it thinks you are touching, etc. This can really help figure out collider sizes, etc.

avatar image GamingC3 whogas · Sep 16, 2020 at 11:42 AM 0
Share

@whogas 1. No, it happens randomly. Pretty sure they're not interfering 2. Nope its completely random 3. Yes, i did tried turning them on and look for my self i even recorded the screen and watched it frame by frame but i didn't found anything unusual. Also i was i apologize for the late replies as i was busy with some service work. This is the only bug that is stopping me from publishing my game. Eveything else is in place and working as intended

avatar image whogas · Sep 16, 2020 at 02:30 PM 0
Share

Since you have the DragAndDrop script on every puff, you aren't making sure that the inputs are being applied to that specific puff. There is a check within the switch statement when the touch begins, but the movement and end don't have any checks. You should probably add the check prior to the switch statement. The way it works right now, it seems like it should all work out in the end, but you may have some weird cross-talk as each puff runs its update.

I would set up a test case and observe that touchphase.ended is firing when you expect it to once you have the touch check tied to the touched puff only.

A note: I would set a variable for Camera.main. Right now, you are searching the entire stack for the camera each update cycle for each puff. This can be avoided by just setting it in Start with the rest of your assignments.

avatar image GamingC3 whogas · Sep 16, 2020 at 02:38 PM 0
Share

i actually tried something similar but failed miserably with the code. now that u've suggested it, i feel like i should give it another go. I'll let you know the results.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ArminAhmadi · Sep 16, 2020 at 04:09 PM

Try this and see if your problem gets fixed.

 public class DragAndDrop : MonoBehaviour
 {
 
     private Collider2D col = null;
     private int flag = 0;
     private int fingerId = -1;
     private GameMaster gm = null;
 
     private AudioSource audioSource;
     public GameObject selectEffect;
     public GameObject deathEffect;
     public GameObject electricDeath;
     public GameObject poisonDeath;
     public GameObject fireDeath;
     public GameObject frostDeath;
 
     private void Start()
     {
         gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
         audioSource = GetComponent<AudioSource>();
         col = GetComponent<Collider2D>();
     }
 
     private void Update()
     {
         if (Input.touchCount > 0 && !IsPointerOverUIObject())
         {
             for (int i = 0; i < Input.touchCount; i++)
             {
                 Touch touch = Input.GetTouch(i);
                 Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
                 Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
                 if (col == touchedCollider)
                 {
                     if (fingerId >= 0)
                     {
                         if (fingerId == touch.fingerId)
                         {
                             if (touch.phase == TouchPhase.Moved && flag == 1)
                             {
                                 transform.position = new Vector2(touchPosition.x, touchPosition.y);
                             }
                             else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
                             {
                                 flag = 0;
                                 fingerId = -1;
                             }
                         }
                     }
                     else
                     {
                         if (touch.phase == TouchPhase.Began)
                         {
                             Instantiate(selectEffect, transform.position, Quaternion.identity);
                             audioSource.Play();
                             fingerId = touch.fingerId;
                             flag = 1;
                         }
                     }
                 }
             }
         }
         else
         {
             fingerId = -1;
             flag = 0;
         }
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "Planet" || collision.tag == "Projectile")
         {
             Instantiate(deathEffect, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
         if (collision.tag == "ElectricOrb")
         {
             Instantiate(electricDeath, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
         if (collision.tag == "Spiky")
         {
             Instantiate(poisonDeath, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
         if (collision.tag == "FireOrb")
         {
             Instantiate(fireDeath, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
         if (collision.tag == "FreezeOrb")
         {
             Instantiate(frostDeath, transform.position, Quaternion.identity);
             Destroy(gameObject);
             gm.GameOver();
         }
     }
Comment
Add comment · Show 2 · 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 GamingC3 · Sep 16, 2020 at 06:39 PM 0
Share

yep this works as i want but the only problem it gave is that it allows multi-touch, i want to keep it as a single touch. im not sure which variable or sign i should change for single touch , also i dont wanna mess it up.

avatar image ArminAhmadi · Sep 17, 2020 at 05:37 PM 1
Share

I have noticed that you have a Game$$anonymous$$aster script.

 private Game$$anonymous$$aster gm = null;

Create a public int in that script and name it activeFingerId. Then change Update function like this.

 private void Update()
 {
     if (Input.touchCount > 0 && !IsPointerOverUIObject())
     {
         for (int i = 0; i < Input.touchCount; i++)
         {
             Touch touch = Input.GetTouch(i);
             Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
             Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
             if (col == touchedCollider)
             {
                 if (fingerId >= 0)
                 {
                     if (fingerId == touch.fingerId)
                     {
                         if (touch.phase == TouchPhase.$$anonymous$$oved && flag == 1 && gm.activeFingerId == fingerId)
                         {
                             transform.position = new Vector2(touchPosition.x, touchPosition.y);
                         }
                         else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
                         {
                             if(gm.activeFingerId == fingerId)
                             {
                                 gm.activeFingerId = -1;
                             }
                             flag = 0;
                             fingerId = -1;
                         }
                     }
                 }
                 else
                 {
                     if (touch.phase == TouchPhase.Began && gm.activeFingerId < 0)
                     {
                         Instantiate(selectEffect, transform.position, Quaternion.identity);
                         audioSource.Play();
                         fingerId = touch.fingerId;
                         flag = 1;
                         gm.activeFingerId = fingerId;
                     }
                 }
             }
         }
     }
     else
     {
         fingerId = -1;
         flag = 0;
     }
 }

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

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

InputSystem simulated touchscreen stops working from v1.1.0 preview 1 0 Answers

Problem with random chess piece moveplate generator 2 Answers

Infuriating bug: Android build occasionally dropping touch input. 0 Answers

Touch.deltaPosition giving jerky movement on Android in Unity 5 2 Answers

Unity Bug, Letting User To touch two UI Buttons as same time (simultaneously) 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