rotate position.y by touch

so i want rotate the y position of my object.
i’ve already read some question like mine, but i’m still cant get it work.

this is my code.

 
    public GameObject target;
    public float speed = 5f;

	// Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch theTouch = Input.GetTouch(0);
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(theTouch.position);

                if (theTouch.phase == TouchPhase.Began)
                {
                    if (Physics.Raycast(ray, out hit))
                    {
                        target.transform.Rotate(0, theTouch.deltaPosition.x * speed, 0, Space.World);
                    }
                }
        }
    }

is there a problem with my script?
i appreciate all you help. Thank for read.

If you just want to rotate an object by touch(and not having to hit it with your finger), then you should ditch the whole raycast thing, cause right now you are checking if you hit something, and only then executing, with the code:

 if (Physics.Raycast(ray, out hit))

If what you want to do is rotate it by swiping the screen, try to use Touch.deltaTime(it records the distance the finger has travelled since last frame, and thus, the speed).

You can of course also use this if you want to click on an object, but then you should make a seperate selecting(and maybe un selecting) code(and not make the rotate be dependant on the finger to be on top of the object).