The movement of my player depends on swipes,
swipe left-> character moves to the left
swipe right-> character moves to the right
right now i am only getting a small movement, meaning that the player moves along the X axis but it stops inmediatelly. This is the code i have to check the swipes direction:
PLayer.cs:
public Vector2 desiredPosition;
void Update()
{
if (MobileInput.Instance.SwipeIzquierda)
{
desiredPosition += Vector2.left;
}
if (MobileInput.Instance.SwipeDerecha)
{
desiredPosition += Vector2.right;
}
transform.position. = Vector2.MoveTowards(transform.position, desiredPosition, moveSpeed * Time.deltaTime);
In the MobileInput script i control the swipes:
private const float DEADZONE = 100.0f;
public static MobileInput Instance { set; get; }
private bool swipeIzquierda, swipeDerecha, swipeAbajo, swipeArriba;
private Vector2 swipePuntoAhora, touchComienzo;
public bool SwipeIzquierda { get { return swipeIzquierda; } }
public bool SwipeDerecha { get { return swipeDerecha; } }
public bool SwipeArriba { get { return swipeArriba; } }
public bool SwipeAbajo { get { return swipeAbajo; } }
private void Awake()
{
Instance = this;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
private void Update () {
Debug.Log(SwipeIzquierda);
swipeDerecha = swipeIzquierda = false;
#region Input Pc
if (Input.GetMouseButtonDown(0))
{
touchComienzo = Input.mousePosition;
}
else if(Input.GetMouseButtonDown(0))
{
touchComienzo = swipePuntoAhora = Vector2.zero;
}
#endregion
#region Mobile Input
if (Input.touches.Length != 0)
{
Debug.Log("hola");
if (Input.touches[0].phase == TouchPhase.Began)
{
touchComienzo = Input.mousePosition;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
touchComienzo = swipePuntoAhora = Vector2.zero;
}
}
#endregion
#region Calcular distancia
swipePuntoAhora = Vector2.zero;
if (touchComienzo != Vector2.zero)
{
//Mobile
if (Input.touches.Length != 0)
{
swipePuntoAhora = Input.touches[0].position - touchComienzo;
}
//PC
else if (Input.GetMouseButton(0))
{
swipePuntoAhora = (Vector2)Input.mousePosition - touchComienzo;
}
}
#endregion
#region Calcular DEadzone del swipe
if (swipePuntoAhora.magnitude > DEADZONE)
{
float x = swipePuntoAhora.x;
float y = swipePuntoAhora.y;
if (Mathf.Abs(x) > Mathf.Abs(y))
{
//Izquierda o Derecha
if (x < 0)
{
swipeIzquierda = true;
}
else
{
swipeDerecha = true;
}
}
else
{
//Arriba o Abajo
if (y < 0)
{
swipeAbajo = true;
}
else
{
swipeArriba = true;
}
}
touchComienzo = swipePuntoAhora = Vector2.zero;
}
#endregion
}
The goal of all of this is to:
If i swipe to te right, the player starts to walk to the right AND continues doing so unless i swipe to the left, only then the player starts to move to the left. I want the movement to be constant in one direction until the other swipe changes it
Any help with this will be really appreciated