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!
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;
}
}
}
}
}
}