Problem with the touch control

Hello,

The game I’m developing is a 2D shoot em up and I’m making a script to control a spaceship but it gives me some errors!

The scripts works like that: When you touch the left side of the screen (button A) the spaceship goes up, and when you touch the right side (button B) the spaceship goes down. If you don’t touch the screen, the spaceship just advance normally.

Here’s the situation. I hold the A button and the ship goes up, then I hold the B button without releasing A button and the ship goes down. When I release A button the ship still continues going down because I’m already holding B button. Then (and the problem goes now) when I hold A button again the ship don’t go up.

I think that the problem comes from the amount of times that the screen gets touched (TouchCount).

Here is a little sketch to make it more clear. Red button is for showing that the button is hold!
alt text

I will be very glad if someone wants to give me some advice.

Thanks!

using UnityEngine;
using System.Collections;

public class ShipControl: MonoBehaviour {

    public float Speed = 8.0f; 
    private Transform MyTransform;

    private enum Position
    {
        Up, Down, Center
    } ;

    private Position ShipPosition = Position.Center;
    private float ValueY;
    private Touch Touch1;
    private int tapCount;
    private int i;
    // Start para inicializar. 
    void Start ()
    {
        MyTransform = transform;
    }

    void Update () {

        if(ShipPosition == Position.Center)
        {

            MyTransform.Translate(new Vector2(Speed, 0) * Time.deltaTime);
        }
        else
        {
            MyTransform.Translate(new Vector2(Speed, ValueY) * Time.deltaTime);
        }

        ControlFly();   
    }


    void ControlFly()
    {   

        if(Input.touchCount > 0)
        {

            for(i = 0; i < Input.touchCount; i++)
            {
                Touch1 = Input.GetTouch(i);
            }


            if((Touch1.phase == TouchPhase.Ended || Touch1.phase == TouchPhase.Canceled))
            {
                iTween.RotateTo(gameObject, iTween.Hash("z", 0, "time", 0.3f, "easetype", "easeinoutsine"));
                ShipPosition = Position.Center; 

            }

            if(Touch1.position.x < Screen.width/2)
            {               
                if((Touch1.phase == TouchPhase.Stationary) || (Touch1.phase == TouchPhase.Moved))
                {
                    ShipPosition = Position.Up;
                    iTween.RotateTo(gameObject, iTween.Hash("z", 15, "time", 0.3f, "easetype", "easeinoutsine"));
                    ValueY = 10;
                }
            }
            else
            {
                if(Touch1.position.x >Screen.width/2)
                {
                    if((Touch1.phase == TouchPhase.Stationary) || (Touch1.phase == TouchPhase.Moved))
                    {
                        ShipPosition = Position.Down;
                        iTween.RotateTo(gameObject, iTween.Hash("z", -15, "time", 0.3f, "easetype", "easeinoutsine"));
                        ValueY = -10;
                    }
                }
            }
        }
    }

}

Thanks a lot for the answer, but the problem is that it don’t recognize the last touch when both buttons are pressed, i have changed your code in order to get the last touch, but the problem persist like in the picture.

if(touchingLeft && touchingRight)
			{
				for(int i = 0; i < Input.touchCount; i++)
				{
					 MyTouch = Input.GetTouch(i);
				}
				
				if(MyTouch.position.x < Screen.width / 2)
				{
					ShipPosition = Position.Up;
					iTween.RotateTo(gameObject, iTween.Hash("z", 15, "time", 0.3f, "easetype", "easeinoutsine"));
					ValueY = 10;
				}
				else if(MyTouch.position.x > Screen.width /2)
				{
					ShipPosition = Position.Down;
					iTween.RotateTo(gameObject, iTween.Hash("z", -15, "time", 0.3f, "easetype", "easeinoutsine"));
					ValueY = -10;
				}
			}

Hey canochaba :slight_smile:

Did you try to debug this, and see exactly what’s happening?

// Variable used to avoid showing a log in each update
int touchCount;

void ControlFly()
{
  if (touchCount != Input.touchCount) {
    touchCount = Input.touchCount;
    // Does the correct amount of touches show?
    // If not, we can stop here since it's a Unity bug
    Debug.Log("Tot touches: " + touchCount);
  }
  if (touchCount == 0) return;
  // If there are touches,
  // try getting the one you want to use
  // using your preferred left to right precedence
  float screenHalfW = Screen.width * 0.5f;
  Touch activeTouch;
  for(int i = 0; i < touchCount; i++) {
    Touch touch = Input.GetTouch(i);
    if (touch.phase != TouchPhase.Stationary || touch.phase != TouchPhase.Moved) continue;
    if (touch.position.x < screenHalfW || activeTouch == null) activeTouch = touch;
  }
  if (activeTouch != null) {
    // You got an active touch, with left touches
    // taking precedence over right touches
    // YOUR CODE
  } else {
    // Touch ended or touch cancelled
    // YOUR CODE
  }
}

I wrote this directly here, so I hope I made no typos :stuck_out_tongue: Also, I never used Touch inputs before, but I was wondering why you aren’t taking into account the TouchPhase.Began?