1

So in trying to learn more about mobile game development I coded a simple game of snake using C#/Unity. I have been having trouble getting the movement to work properly on a mobile (android) device and as of now this is the code that works when playing through Unity using a keyboard. How would I go about converting this code to work in a similar fashion but on a mobile device with the input being swipes or taps on the phone screen by the user?

 void Movement()
{
    GameObject temp;
    nexPos = head.transform.position;
    switch (NESW)
    {
        case 0:
            nexPos = new Vector2(nexPos.x, nexPos.y + 1);
            break;
        case 1:
            nexPos = new Vector2(nexPos.x + 1, nexPos.y);
            break;
        case 2:
            nexPos = new Vector2(nexPos.x, nexPos.y - 1);
            break;
        case 3:
            nexPos = new Vector2(nexPos.x - 1, nexPos.y);
            break;
    }
    temp = (GameObject)Instantiate(snakePrefab, nexPos, transform.rotation);
    head.Setnext(temp.GetComponent<Snake>());
    head = temp.GetComponent<Snake>();

    return;
}

void changeDirection()
 {
    if (NESW != 2 && Input.GetKeyDown(KeyCode.W))
    {
        NESW = 0;
    }
    if (NESW != 3 && Input.GetKeyDown(KeyCode.D))
    {
        NESW = 1;
    }
    if (NESW != 0 && Input.GetKeyDown(KeyCode.S))
    {
        NESW = 2;
    }
    if (NESW != 4 && Input.GetKeyDown(KeyCode.A))
    {
        NESW = 3;
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

0 Answers0