Camera swipe AND gameObject touch (Android)

Hello!
I’m working on a 2D android game and I’m currently trying to make a functional main menu for the game.

Right now I’m trying to make a swipe effect that simply scrolls through a list objects and then the user can tap on one of these objects and that will load the level.
I made a script to detect the touch on a specific GameObject and also a simple script for the swipe effect.

The touch detection part is pretty simple and easy to use, the swipe part simply moves the camera up/down and clamps its position to the same position of the first and last object of the list.

It all works BUT my problem is that when I swipe the finger on the screen to scroll the list, if a button is hit, the level loading script starts instead of just keep going with the swipe.

Here’s my code:
function FixedUpdate () {

	if(Input.touchCount ==1 && Input.GetTouch(0).phase == TouchPhase.Stationary) {

          var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
          var hit : RaycastHit;
       if (Physics.Raycast (ray, hit)) {
       		hit.transform.gameObject.SendMessage('Clicked',0,SendMessageOptions.DontRequireReceiver);
          
         }
       }
       
    else{
	       	if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved && scrollable) {
				scroll();
				
			}
       }
    
 
}

function scroll(){
		var maxYcamera=0;
		var minYcamera=botGameObject.transform.position.y+2;
		// Get movement of the finger since last frame
				var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
				
				
				// Move object across XY plane
				transform.Translate (0,-touchDeltaPosition.y * speed, 0);
				transform.position.y= Mathf.Clamp(transform.position.y, minYcamera, maxYcamera);
}

It should be simple: IF there’s one touch AND the finger is stationary on that point —> do your thing; ELSE if the finger is moving —> swipe.
I don’t get why it execute the first part of code even if the finger is moving (I know the swiping works because, if the finger doesn’t touch a button, it swipes correctly).

I also tried to change the Input.GetTouch(0).phase == TouchPhase.Stationary to .Began or .Ended but with no results.

Does anyone know what the problem could be?

I think what you need is a variable to check if a finger is moved or not and keep it true until the finger is lifted. I added a “canClick” variable that is set to false after “scroll()” is called. I then added a “canClick” check to the raycast if statement for clicked. Once scrolling has started, the button can’t be clicked until “TouchPhase.Ended”.

This should work, let me know =)


var canClick : boolean = true;

	if(Input.touchCount ==1 && Input.GetTouch(0).phase == TouchPhase.Stationary) {
 		if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved && scrollable) {
          scroll();
          canClick=false;
		}
        var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit) && canClick) {
            hit.transform.gameObject.SendMessage('Clicked',0,SendMessageOptions.DontRequireReceiver);
 		}
	}
	if (touch.phase == TouchPhase.Ended) {
		canClick=true;
	}
}

function scroll(){
       var maxYcamera=0;
       var minYcamera=botGameObject.transform.position.y+2;
       // Get movement of the finger since last frame
       var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;

       // Move object across XY plane
       transform.Translate (0,-touchDeltaPosition.y * speed, 0);
       transform.position.y= Mathf.Clamp(transform.position.y, minYcamera, maxYcamera);
}