Touchscreen controls working on x axis but not y axis

Hello,

I’m very new to working with mobile devices currently and was trying to get my camera to move based on users touching the top, bottom, left, or right portions of the screen.

So far, I have it working perfectly if I touch (and hold) the left and right portions of my phones screen, however when I do the same thing for the height of my screen (i.e. top and bottom), my code doesn’t work.

I was wondering if someone could take a look at my code and tell me why the “y-axis”/height/top-bottom code isn’t working?

using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour {

    private Transform thisTransform;

    public float cameraSpeed = 10;



    // Use this for initialization
    void Start () {
        thisTransform = transform;
    }



    // Update is called once per frame
    void Update () {

        if (Input.touchCount > 0) {
            if (Input.GetTouch(0).position.x <= Screen.width*.20f && Input.GetTouch (0).phase == TouchPhase.Stationary) {

                thisTransform.Translate (new Vector3 (-1, 0, 0) * cameraSpeed * Time.deltaTime, Space.World);

            }


            if (Input.GetTouch(0).position.x >= Screen.width*.80f && Input.GetTouch (0).phase == TouchPhase.Stationary) {
               
                thisTransform.Translate(new Vector3(1, 0, 0) * cameraSpeed * Time.deltaTime, Space.World);
               
            }


            if (Input.GetTouch(0).position.y <= Screen.height*.20f && Input.GetTouch (0).phase == TouchPhase.Stationary) {
               
                thisTransform.Translate (new Vector3(0, 0, -1) * cameraSpeed * Time.deltaTime, Space.World);
               
            }


            if (Input.GetTouch(0).position.y <= Screen.height*.80f && Input.GetTouch (0).phase == TouchPhase.Stationary) {
               
                thisTransform.Translate (new Vector3(0, 0, 1) * cameraSpeed * Time.deltaTime, Space.World);
               
            }
        }
    }

}

Sorry if the code insertion formatting is a little hard to read.

Thanks!

I figured it out :slight_smile:

My last condition check was “less than or equal to” which is the same as the previous condition check, and so cancelling each other out. My last condition check should be “greater than or equal to”.

Changed it and it works. :roll_eyes: