user4456470
user4456470

Reputation: 16

Android Game built with unity suddenly stops accepting touch input after a while

I have a game made with unity and built for Android. On the run window on Unity, the code works okay. However, on the android device, the touch input is registered for a while before suddenly stopping. What could be the issue?

I have tried debugging with Android monitor and no errors are being shown. I have tried building with an old version of the code, still the same. The error can be more clearly seen on the build on playstore google, but i would not want to break community rules here. PM if it might help

the multiple moving code is to check if the player is already moving so as to allow the player to move multiple tiles in the grid at once.

void setStartMove()
    {
        if (!startMove){        
            startMove = true;    
        }

    }




 public void Update()
    {

        myMap.getTileNeighbors(playerTile);
        if (neighbor == null)
        {
            neighbor = playerTile;
        }
        if (moving && (transform.position == endpos))
        {
            moving = false;
        }
        if (moving && startMove && (transform.position != endpos))
        {
            gameController.addMovesTaken(1);
            levelChanger.updateInGameMoves();
            startMove = false;
        }


        if (!moving && swipeControls.SwipeUp)
        {
            neighbor = myMap.getLastMoveableTile(playerTile, "up");
            moving = true;
            setStartMove();
        }


        if (!moving && swipeControls.SwipeDown)
        {
            neighbor = myMap.getLastMoveableTile(playerTile, "down");
            moving = true;
            setStartMove();
        }



        if (!moving && swipeControls.SwipeLeft)
        {
            neighbor = myMap.getLastMoveableTile(playerTile, "left");
            moving = true;
            setStartMove();
        }


        if (!moving && swipeControls.SwipeRight)
        {
            neighbor = myMap.getLastMoveableTile(playerTile, "right");
            moving = true;
            setStartMove();

        }

        Vector3 neighborPosCorr = new Vector3(neighbor.worldPosition.x, transform.position.y, neighbor.worldPosition.z);
        endpos = neighborPosCorr;

        transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * moveSpeed);

        playerTile = myMap.GetTileFromWorldPoint(transform.position);
    }



and the SwipeControls

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwipeController : MonoBehaviour
{

    private PlayerManager playerManager;

    private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    private bool isDraging = false;
    private Vector2 startTouch, swipeDelta;

    private float deltaBorder = 15.0f;

    public Vector2 SwipeDelta { get { return swipeDelta; } }
    public bool Tap { get { return tap; } }
    public bool SwipeLeft { get { return swipeLeft; } }
    public bool SwipeRight { get { return swipeRight; } }
    public bool SwipeUp { get { return swipeUp; } }
    public bool SwipeDown { get { return swipeDown; } }

    public bool Dragging { get { return isDraging; } }
    private void Awake()
    {
        playerManager = GameObject.Find("GameManager").GetComponent<PlayerManager>();
    }
    private void Update()
    {
        tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;

        #region Standalone Inputs
        if (Input.GetMouseButtonDown(0))
        {
            tap = true;
            isDraging = true;
            startTouch = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            isDraging = false;
            Reset();
        }
        #endregion

        #region Mobile Input
        if (Input.touches.Length > 0)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                isDraging = true;
                tap = true;
                startTouch = Input.touches[0].position;
            }
            else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
            {
                isDraging = false;
                Reset();
            }
        }
        #endregion

        // Calculate the distance
        swipeDelta = Vector2.zero;
        if (isDraging)
        {
            if (Input.touches.Length > 0)
                swipeDelta = Input.touches[0].position - startTouch;
            else if (Input.GetMouseButton(0))
                swipeDelta = (Vector2)Input.mousePosition - startTouch;
        }

        //Did we cross the distance?
        if (swipeDelta.magnitude > deltaBorder)
        {
            //Which direction?
            float x = swipeDelta.x;
            float y = swipeDelta.y;
            if (Mathf.Abs(x) > Mathf.Abs(y))
            {
                //Left or right
                if (x < 0)
                    swipeLeft = true;
                else
                    swipeRight = true;
            }
            else
            {
                // Up or down
                if (y < 0)
                    swipeDown = true;
                else
                    swipeUp = true;
            }
            Reset();
        }

    }

    void Reset()
    {
        startTouch = swipeDelta = Vector2.zero;
        isDraging = false;
    }
}


Upvotes: 0

Views: 372

Answers (1)

user4456470
user4456470

Reputation: 16

The issue was with the multiple recursive calls in the update. Reengineered code to reduce the recursion, and moved some of it to late update

Upvotes: 0

Related Questions