I have an Android game where user moves player by touching left and right side of the screen. I tried the game on Android and all worked OK. But one day it stopped working: buttons work but I can't control the Player by touching screen sides. I tried to reinstall Unity, delete Library folder and even tried other OS. It didn't help. How can I fix my project?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MovingPlayer : MonoBehaviour
{
public float playerSpeed;
public float maxPos = 2.7f;
Vector3 position;
public uiManager ui;
public AudioManager am;
public Text coinCount;
int coin;
bool currntPlatformAndroid = false;
Rigidbody2D rb;
public GameObject particleSystemPrefab;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
#if UNITY_ANDROID
currntPlatformAndroid = true;
#else
currentPlatformAndroid = false;
#endif
am.playerSound.Play();
}
// Use this for initialization
void Start()
{
coin = 0;
position = transform.position;
}
// Update is called once per frame
void Update()
{
if (currntPlatformAndroid == true)
{
TouchMove();
//AccelerometerMove();
}
else
{
position.x += Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime;
position.x = Mathf.Clamp(position.x, -2.7f, 2.7f);
transform.position = position;
}
position = transform.position;
position.x = Mathf.Clamp(position.x, -2.7f, 2.7f);
transform.position = position;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "CubeObstacle")
{
//Destroy (gameObject);
foreach (ContactPoint2D contact in col.contacts)
{
Instantiate(particleSystemPrefab, contact.point, Quaternion.identity);
}
//gameObject.SetActive(false);
GameOverActivated();
ui.gameOverActivated();
am.playerSound.Stop();
}
if (col.gameObject.tag == "Coin")
{
Destroy(col.gameObject);
coin += 1;
coinCount.text = coin.ToString();
Debug.Log("Coin");
}
}
public void GameOverActivated()
{
Time.timeScale = 0;
PlayerPrefs.SetInt("coins", PlayerPrefs.GetInt("coins") + coin);
}
void AccelerometerMove()
{
float x = Input.acceleration.x;
if (x < -0.1f)
{
MoveLeft();
}
else if (x > 0.1f)
{
MoveRight();
}
else
{
SetVelocityZero();
}
}
void TouchMove()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
float middle = Screen.width / 2;
if (touch.position.x < middle && touch.phase == TouchPhase.Began)
{
MoveLeft();
}
else if (touch.position.x > middle && touch.phase == TouchPhase.Began)
{
MoveRight();
}
}
else
{
SetVelocityZero();
}
}
public void MoveLeft()
{
rb.velocity = new Vector2(-playerSpeed, 0);
}
public void MoveRight()
{
rb.velocity = new Vector2(playerSpeed, 0);
}
public void SetVelocityZero()
{
rb.velocity = Vector2.zero;
}
}