Hello,
I’m trying to make a simple snake game but stuck in mobile touch input. When I press right it should go right but instead the snake is moving diagonally.
Any support?
Thanks
void TouchMovement()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
Vector2 touchPosition = Input.GetTouch(0).position;
float halfScreenH = Screen.width / 2.0f;
float halfScreenV = Screen.height / 2.0f;
//Check if it is left or right?
if (touchPosition.x < halfScreenH)
{
//snake.transform.Translate(Vector3.left * 2 * Time.deltaTime);
snake.transform.Translate(new Vector2(1,0) * 2 * Time.deltaTime);
Debug.Log("Moving left");
}
else if (touchPosition.x > halfScreenH)
{
snake.transform.Translate(new Vector2(-1, 0) * 2 * Time.deltaTime);
Debug.Log("Moving right");
}
//Check if it is up or down?
if (touchPosition.y < halfScreenV)
{
snake.transform.Translate(new Vector2(0,-1) * 2 * Time.deltaTime);
Debug.Log("Moving Up");
}
else if (touchPosition.y > halfScreenV)
{
snake.transform.Translate(new Vector2(0,1) * 2 * Time.deltaTime);
Debug.Log("Moving Down");
}
}
}