Touchscreen input: Moving the player using screen.width & screen.height

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");

                }

            }
        }

The way you have it now is not just listening for right or left, but also up and down, so diagonal movement is expected. If you don’t want diagonal movement you might want to consider using buttons, a swiping gesture, or some way to determine what axis should be used like I have done below because right now, it will always move diagonally because a vertical and horizontal movement always happens in your code:

float halfX;
float halfY;

void Start()
{
    halfX = Screen.width * 0.5f
    halfY = Screen.height * 0.5f;
}

void Update()
{
    if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
    {
        Vector2 touchPos = Input.GetTouch(0).position;
        float horizontalWeight = Mathf.Abs(halfX - touchPos.x);
        float verticalWeight = Mathf.Abs(halfY - touchPos.y);
        
        if(horizontalWeight > verticalWeight)
        {
            if(touchPos.x < halfX)
                snake.transform.Translate(new Vector2(1,0) * 2 * Time.deltaTime);

            else
                snake.transform.Translate(new Vector2(-1,0) * 2 * Time.deltaTime);
        }
        else
        {

        if(touchPos.y > halfY)
            snake.transform.Translate(new Vector2(0,1) * 2 * Time.deltaTime);

        else
            snake.transform.Translate(new Vector2(0,-1) * 2 * Time.deltaTime);
         }
    }
}