creating a touch area in unity

Hello guys, i’m developing an fps game on smartphone and got stuck on the commands. Basically what i wanted to do was to use the left area of the smartphone’s screen just to look around(so camera movements), so i created a canvas as a touch area(named “ta” in the script) but it doesn’t work. Without declaring the touch inputs inside the if [if (ta.GetComponent().Contains(t.position))] camera commands works just fine but i’d like it to to work only in the left half of the screen( maybe in a canvas/panel).

Here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Mov : MonoBehaviour
{
    public Joystick joystickCamera;
    public GameObject player;
    public Camera thisC;
    private Touch we = new Touch();
    public float speed = 0.1f, moveSpeed;
    public bool pressed = false;
    Transform cameraTransform;
    public GameObject ta;
    private float rotX =0f;
    private float rotY =0f;
    public GameObject testo;

    // Start is called before the first frame update
    void Start()
    {
        testo = GameObject.FindGameObjectWithTag("t");
        ta = GameObject.FindGameObjectWithTag("bU");
        cameraTransform = Camera.main.transform;
        joystickCamera = Joystick.FindObjectOfType<Joystick>();
        player = GameObject.FindGameObjectWithTag("Player");

        testo.GetComponent<Text>().text = "ASD";
        //Screen.lockCursor = true;
        //cameraTransform.eulerAngles = new Vector3(Mathf.Clamp(cameraTransform.eulerAngles.x, -90, 90), 0, 0);
    }
    /// </summary>
    // Update is called once per frame
    void Update()
    {
        foreach (Touch t in Input.touches) {
            if (ta.GetComponent<Rect>().Contains(t.position)) {
                testo.GetComponent<Text>().text = "HELP";
                //Debug.Log("fff");
            //if () {
                if (t.phase == TouchPhase.Began)
                {
                    we.position = t.position;
                }
                if (t.phase == TouchPhase.Moved)
                {
                    float deltaX = we.position.x - t.position.x;
                    float deltaY = we.position.y - t.position.y;
                    rotX -= deltaX * Time.deltaTime * speed;
                    rotY += deltaY * Time.deltaTime * speed;
                    //this.transform.eulerAngles = new Vector3(rotY, rotX, 0f);
                    thisC.transform.eulerAngles = new Vector3(rotY, rotX, 0f);
                    //player.transform.Rotate(0, thisC.transform.rotation.y, 0);
                    player.transform.eulerAngles = new Vector3(0f, rotX, 0f);
                }
            }
        }
        /*player.transform.Rotate(0, Input.GetAxis("Mouse X") * speed * Time.fixedDeltaTime,0);
        this.transform.Rotate(-Input.GetAxis("Mouse Y") * speed * Time.fixedDeltaTime, 0,0);*/

        if (pressed)
        {
            player.transform.Translate(Vector3.forward * moveSpeed);
        }
    }

    public void onPointerDownRaceButton(UnityEngine.EventSystems.BaseEventData eventData)
    {
        pressed = true;
    }
    public void onPointerUpRaceButton(UnityEngine.EventSystems.BaseEventData eventData)
    {
        pressed = false;
    }
}

Any help is really appreciated.

if (ta.GetComponent<Rect>().Contains(t.position)) should look for a RectTransform instead of a Rect, so you could then do if (ta.GetComponent<RectTransform>().rect.Contains(t.position)).
You will have to do a conversion between screen space and the rect’s local space. You can use the RectTransform’s InverseTransformPoint method I think. Unity - Scripting API: Transform.InverseTransformPoint

Actually, you should probably declare your “ta” variable to be of type “RectTransform” in the first place, and call GetComponent when you are initializing it rather than every time you need to reference the component.

If Wallace is correct about the problem (and that looks right to me), then you probably also have error messages in the Unity console from the null reference exceptions you’d be getting when you try to call .Contains on the Rect component that Unity will have failed to find.

Use the modern EventSystem to do inputs like these. For example, take a look at IDragHandler.