Can I limit touch input for camera movement to non-ui areas?

Hi there

First I have two cameras. One for the UI/HUD and the MainCamera both on different layers. I can control the camera by touch input - pinch to zoom and swipe to orbit. Now when I use the 2D UI elements/controls the camera also moves around because I am using the Input.GetTouch() method for the camera script. What I try to do is limit the touch input for the camera movement to “non-UI” areas so that the UI is in front and has priority.

I read about CullingMask but I don’t know how to use this to solve my problem.
Is there a way to “filter” the colliders of the UI? I always think this can not be that difficult, however I’m stuck.

Thank you for any help!

Everyone hates this problem! There is no really good solution.

If Unity worked the “other” way, it would be just as bad.

I feel the simplest approach is:

in your ORBIT script. simply check if you are indeed hitting one of the colliders of the UI. if so, ignore it and do nothing.

if your UI is confined to a specific simple area (say “the top 1/4 of the screen”) … just make one collider called “UI exclusion zone”. and again in your game controls, just check if you are hitting that and if so, just ignore that touch.

Hope it helps

PS - I notice you are a new user. Be sure to “Tick” any useful answers please. You get points, and can this ask more questions in the future

The below code work on my project. I was able to limit the movement of my object.

    public GameObject myObject;

float yMin = -3.84f;
float yMax = 3.84f;

void Update()

	if (Input.touchCount > 0 && transform.position.y <= yMax && transform.position.y >= yMin)
		Touch touch = Input.GetTouch(0);

		Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
		myObject.transform.position = new Vector3(-9f, touchPosition.y, transform.position.z);

		if (transform.position.y >= yMax)
			myObject.transform.position = new Vector3(-9f, 3.83f, 0);

		if (transform.position.y <= yMin)
			myObject.transform.position = new Vector3(-9f, -3.83f, 0);