title: Player want to move left and right from single - touch (Unity)
body: <p>I made the game in Unity Space Shooter. In my Space shooter there is 2 button it work for Left and Right moving. I want when we touch the left button player go to left only in Single Touch same like Right Button also.</p>
  
  <p>This , are the some codes which i used in Game. Please Help me out from this.</p>
  
  <p>TouchControl.cs</p>
  
  
  
  <pre class="lang-c# prettyprint-override"><code>using UnityEngine;
  using System.Collections;
  
  public class TouchControl : MonoBehaviour {
  
      public GUITexture moveLeft;
      public GUITexture moveRight;
      public GUITexture fire;
      public GameObject player;
      private PlayerMovement playerMove;
      private Weapon[] weapons;
  
      void Start()
      {
          playerMove = player.GetComponent&lt;PlayerMovement&gt; ();
      }
  
      void CallFire()
      {
          weapons = player.GetComponentsInChildren&lt;Weapon&gt; ();
          foreach (Weapon weapon in weapons) {
              if(weapon.enabled == true)
              weapon.Fire();      
          }
      }
  
      void Update()
      {
  //      int i = 0;
          if(Input.touchCount &gt; 0)
          {
              for(int i =0; i &lt; Input.touchCount; i++)
              {
  //          if(moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
  //          {
  //              if(Input.touchCount &gt; 0)
  //              {
  //                  playerMove.MoveLeft();
  //              }
  //          }
  //          if(moveRight.HitTest(Input.GetTouch(i).position, Camera.main))
  //          {
  //              if(Input.touchCount &gt; 0)
  //              {
  //                  playerMove.MoveRight();
  //              }
  //          }
  //          if(moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
  //          {
  //              if(Input.touchCount &gt; 0)
  //              {
  //                  CallFire();
  //              }
  //          }
              //  Touch t = Input.GetTouch(i);
                  Touch t = Input.GetTouch (i);
                  Input.multiTouchEnabled = true;
                  if(t.phase == TouchPhase.Began || t.phase == TouchPhase.Stationary)
                  {
                      if(moveLeft.HitTest(t.position, Camera.main))
                      {
                         playerMove.MoveLeft ();
                  }
                      if(moveRight.HitTest(t.position, Camera.main))
                  {
                          playerMove.MoveRight();
                      }
                  }
                  if(t.phase == TouchPhase.Began)
                  {
                      if(fire.HitTest(t.position, Camera.main))
                      {
                          CallFire();
                      }
                  }
  
  
                  if(t.phase == TouchPhase.Ended)
                  {
  
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>PlayerMovement.cs</p>
  
  <pre class="lang-c# prettyprint-override"><code>using UnityEngine;
  using System.Collections;
  
  public class PlayerMovement : MonoBehaviour {
      public float speedMove = 6.0f;
      public float bonusTime;
  
      private bool toLeft = false;
      private bool toRight = false;
  
      public GameObject shield;
      public GUIText bonustimeText;
  
      private bool counting = false;
      private float counter;
  
      private Weapon[] addWeapons;
  
      public Sprite strongShip;
      public Sprite normalSprite;
      public Sprite shieldSprite;
  
      private SpriteRenderer sRender;
      private Weapon weaponScript;
  
      void Start () {
  
          counter = bonusTime;
  
          sRender = GetComponent&lt;SpriteRenderer&gt; ();
          addWeapons = GetComponentsInChildren&lt;Weapon&gt; ();
          foreach (Weapon addWeapon in addWeapons) {
              addWeapon.enabled = false;
          }
  
          weaponScript = GetComponent&lt;Weapon&gt;();
          weaponScript.enabled = true;
      }
  
      // Update is called once per frame
      void Update () {
  
          if (Input.GetKeyDown (KeyCode.A)) {
              toLeft = true;      
          }
          if (Input.GetKeyUp (KeyCode.A)) {
              toLeft = false;     
          }
          if (Input.GetKeyDown (KeyCode.D)) {
              toRight = true;     
          }
          if (Input.GetKeyUp (KeyCode.D)) {
              toRight = false;        
          }
  
  
          if (counting) {
              counter -= Time.deltaTime;
              bonustimeText.text = counter.ToString("#0.0");
          }
      }
  
  
      void FixedUpdate()
      {
          if (toLeft) {
              MoveLeft();
          }
  
          if (toRight) {  
              MoveRight();
          }
      }
  
  
      public void MoveLeft()
      {
          transform.Translate(Vector2.right * -speedMove* Time.deltaTime);
      }
  
  
      public void MoveRight()
      {
          transform.Translate(Vector2.right * speedMove * Time.deltaTime);
      }
  
  
      void OnCollisionEnter2D(Collision2D coll)
      {
          if (coll.gameObject.tag == "StrongMode") {
              Destroy (coll.gameObject);
              counting = true;
              StrongMode();
              Invoke ("Downgrade", bonusTime);
          }
  
  
          if (coll.gameObject.tag == "ShieldMode") {
              Destroy (coll.gameObject);
              counting = true;
              ShieldMode();
              Invoke("Downgrade", bonusTime);
          }
  
          if (coll.gameObject.tag == "Life") {
              GUIHealth gui = GameObject.Find ("GUI").GetComponent&lt;GUIHealth&gt; ();
              gui.AddHealth();
              SendMessage("AddHp");
              SoundHelper.instanceSound.PickUpSound();
              Destroy(coll.gameObject);
          }
  
          if (coll.gameObject.tag == "Enemy") {
              SendMessage("Dead");
          }
      }
  
      void Downgrade()
      {
          SoundHelper.instanceSound.BonusDownSound ();
          counting = false;
          bonustimeText.text = "";
          counter = bonusTime;
  
          sRender.sprite = normalSprite;
          weaponScript.enabled = true;
          foreach (Weapon addWeapon in addWeapons) {
              addWeapon.enabled = false;
          }
          weaponScript.enabled = true;
          shield.SetActive (false);
      }
  
  
      void StrongMode()
      {
          SoundHelper.instanceSound.BonusUpSound ();
          sRender.sprite = strongShip;
          foreach (Weapon addWeapon in addWeapons) {
              addWeapon.enabled = true;
          }
          weaponScript.enabled = false;
      }
  
  
      void ShieldMode()
      {
          SoundHelper.instanceSound.BonusUpSound ();
          sRender.sprite = shieldSprite;
          shield.SetActive (true);
      }
  
  
  //  void OnDestroy()
  //  {
  //      bonustimeText.text = "";
  //  }
  }
  </code></pre>