Player Movement using Touch Control

I am trying to move two independent players using Input.GetTouch in different parts of the screen. The problem is currently only one can be moved at a time. Is there anyway I can split the screen to handle movement of these separately ?

This is the script for one of the players:

using UnityEngine;
using System;

public class LTouchFollow : MonoBehaviour
{
    public Rigidbody lRb;
    public float sideMax = 5f; //Try screen.width
    public float midlimit = 1f;
    public float playerSpeed = 20f;
    public Rect left = new Rect(0f, 0f, Screen.width/2, Screen.height);

    void Start()
    {
        lRb = GetComponent<Rigidbody>();
    }
    // Update is called once per frame
    public void FixedUpdate()
    {  

        if (Input.touchCount > 0)

        {
            Touch lTouchPx = Input.GetTouch(0);

            if (left.Contains(lTouchPx.position))

            {
                Debug.Log("FuncionaL");


            Vector3 lTouchUnt = Camera.main.ScreenToWorldPoint(lTouchPx.position);

            Vector3 lTargetPosition = new Vector3(lTouchUnt.x, 8f, 0f);
            lTargetPosition.x = Mathf.Clamp(lTargetPosition.x, -sideMax, -midlimit);
            transform.position = Vector3.Lerp(transform.position, lTargetPosition, Time.deltaTime * playerSpeed);

            }
        }
    }
}

Assuming you mean literally split the screen:

Vector3 lTouchUnt = Camera.main.ScreenToWorldPoint(lTouchPx.position);

if(lTouchUnt.x < Screen.width / 2)
{
   // left side of the screen
}
else
{
   // right side of the screen
}

You also shouldn’t be measuring input in FixedUpdate, as it doesn’t necessarily run every frame.

Thanks for your reply,

The problem is I have two different players being controlled via touch drag on the screen simultaneously by the same person. Therefore I have a separate script for each player like the one above. The issue is that currently two independent drag movements are not recognised simultaneously.

I tried splitting the screen as suggested but I keep running into the same problem. How can I handle two independent touch drag movements simultaneously ?

Right now you’re only sampling the first touch with GetTouch(0). You need to loop through all the touches and evaluate them as appropriate. There’s a bunch of examples in the docs: Unity - Scripting API: Input.GetTouch