Dr. Joben
Dr. Joben

Reputation: 33

Limit touch space to half the screen

How do I limit the touch space to the right side of the screen so that it only registers the right side touches. Left side touch nothing will happen. How am I able to do it with this script? Been trying out for the past 2 days still cannot figure anything out. Any experts able to help me out?

public class look2 : MonoBehaviour
{
private Vector3 firstpoint; //change type on Vector3
private Vector3 secondpoint;
private float xAngle = 0.0f; //angle for axes x for rotation
private float yAngle = 0.0f;
private float xAngTemp = 0.0f; //temp variable for angle
private float yAngTemp = 0.0f;
public GameObject character;
void Start()
  {
    //Initialization our angles of camera
    xAngle = 0.0f;
    yAngle = 0.0f;
    this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
    character.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
}
void Update()
{
    //Check count touches
    if (Input.touchCount > 0)
    {
        //Touch began, save position
        if (Input.GetTouch(0).phase == TouchPhase.Began)
        {
            firstpoint = Input.GetTouch(0).position;
            xAngTemp = xAngle;
            yAngTemp = yAngle;
        }
        //Move finger by screen

            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                secondpoint = Input.GetTouch(0).position;
                //Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
                xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
                yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;
                //Rotate camera
                this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
            character.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
          }
        }
    }
 }

Upvotes: 0

Views: 749

Answers (1)

derHugo
derHugo

Reputation: 90862

You can use Screen.width to get exactly that: The width of the device's screen in pixels.

Since the Touch.position is also in screen pixel space the right half of the screen is as simple as any touch having

touch.position.x > Screen.width / 2f

you could also use >= depending whether you want one pixel more or less ;)

And then you could simply filter the touches on that condition before accessing them e.g. using Linq Where

var validTouches = Input.touches.Where(touch => touch.position.x > Screen.width / 2f).ToArray();

This is basically short for doing something like

var touches = new List<Touch>();
foreach(var touch in Input.touches)
{
    if(touch.position.x > Screen.width / 2f)
    {
        touches.Add(touch);
    }
}
var validTouches = touches.ToArray();

So your code could look like

using System.Linq;

...

void Update()
{
    //Check count touches
    if (Input.touchCount > 0)
    {
        // Now collect only touches being on the left half of the screen
        var validTouches = Input.touches.Where(touch => touch.position.x > Screen.width / 2f).ToArray();

        if(validTouches.Length > 0)
        {
            var firstTouch = validTouches[0];

            // Better use switch here
            switch(firstTouch.phase)
            {
                case TouchPhase.Began:   
                    firstpoint = firstTouch.position;
                    xAngTemp = xAngle;
                    yAngTemp = yAngle;
                    break;

                case TouchPhase.Moved:
                    secondpoint = firstTouch.position;
                    //Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
                    xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
                    yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;
                    //Rotate camera
                    this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
                    character.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
                    break;
            }
        }
    }
}

Upvotes: 2

Related Questions