Swipe on the part of the screen

Hi. I have 2 players in my game. I want to make separated controls for them. If I swipe left or right my player would go left or right. But I want same with my second player. So is there any way I can separate controls, for first player I should swipe on the left part of the screen and for the second, I should swipe on the right part of the screen? I already have swipe controls in my script, I only need to detect on which part of the screen player swipes. Greetings!

foreach(Touch touch in Input.touches)
		{
			if (touch.phase == TouchPhase.Began)
			{
				fp = touch.position;
				lp = touch.position;
			}
			if (touch.phase == TouchPhase.Moved )
			{
				lp = touch.position;
			}
			if(touch.phase == TouchPhase.Ended)
			{
				if((fp.x - lp.x) > 80) // left swipe
				{
					
				}
				else if((fp.x - lp.x) < -80) // right swipe
				{
					
				}

Here is a bit of code that uses fingerID and identifies swipe (or at least finger down and finger up) on each side of the screen. I’m sure there is more work to be done, but this should give you the general idea. Create a GUIText object, attach this script and build to your device:

using System.Collections;
using System.Collections.Generic;

[RequireComponent (typeof (GUIText))]
public class FingerSwipe : MonoBehaviour {

	public struct FingerData {

		public FingerData(int f, Vector2 p, float t) {
			fingerID = f;
			startPos = p;
			startTime = t;
		}
		public int fingerID;
		public Vector2 startPos;
		public float startTime; 
	}
	
	private List<FingerData> data = new List<FingerData>();
	private float mid;

	void Start() {
		mid = Screen.width / 2.0f;
	}

	void Update () {

		if (Input.touchCount == 0) {
			data.Clear();
		}
		else {
			foreach (Touch t in Input.touches) {
				if (t.phase == TouchPhase.Began) {
					FingerData d = new FingerData(t.fingerId, t.position, Time.time);
					data.Add (d);

				}
				else if (t.phase == TouchPhase.Ended) {
					int i = data.FindIndex (x => x.fingerID == t.fingerId);
					if (i == -1) {
						Debug.Log ("No matching Begin");
					}
					if (i >= 0) {
						FingerData d = data*;*
  •  				data.RemoveAt(i);*
    
  •  				if (d.startPos.x < mid && t.position.x < mid) {*
    
  •  					guiText.text = d.fingerID.ToString () + ": left side swipe";*
    
  •  				} else if (d.startPos.x >= mid && t.position.x >= mid) {*
    
  •  					guiText.text = d.fingerID.ToString () + ": right side swipe";*
    
  •  				} else {*
    
  •  					guiText.text = d.fingerID.ToString () + ": overlap swipe";*
    
  •  				}*
    
  •  			}*
    
  •  		}*
    
  •  	}*
    
  •  }*
    
  • }*
    }
    I don’t know how your game works, but you may want to establish a certain distance between the finger down and finger up before it becomes a swipe. You may want that distance measured as a fraction of the pixel width of the screen to keep things semi-platform independent. I don’t know your game mechanic, but this code currently allows multiple fingers on each side. So it may be possible for players to “cheat” by swiping with multiple fingers.