Quantcast
Channel: Questions in topic: "unityandroid"
Viewing all articles
Browse latest Browse all 38

Possible bug in unity touch phase for android ?

$
0
0
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][1] 2. [After releasing orange I dragged red fluff. Worked as intended][2] 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][3] 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(); audioSource = GetComponent(); col = GetComponent(); } 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. [1]: https://i.stack.imgur.com/g0qFZ.png [2]: https://i.stack.imgur.com/Hepil.png [3]: https://i.stack.imgur.com/SC2QS.png

Viewing all articles
Browse latest Browse all 38

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>