Grabbing objects for touch controls? how

Hi I need help for unity adding buttons that if you press it, it will grab that object and it will stay on players hand
my player is a stickman ragdoll theirs a picture of how it looks like
it only have 1 arm it doesnt have MHMHmh doesnt have elbow just a stick that attached to the torso
and I need it to grab objects with it like a sword, just a sword or guns etc not the enemies that he can grab only the swords etc also if you uhmmm click the button again it will unattach to the players hand
idk Any coding :smiley: pls help I would reward you with reputation points for sure also pls add if you touch it with mouse pointer it would work so I can actually try it out on pc while its not published yet :smiley:
BUTTONS BUTTONS BUTTON BUTTON CLICK CLICK, FOR MOBILE MOBILE PHONES PHONES

You can use Input.GetTouch to get a Touch struct. One of the parts of this struct is the screen-space coordinate that was touched. You’ll need a bit of extra code (see the linked documentation) for tracking specific fingers and so on, but that’s the basics. You can then use Camera.ScreenToWorldPos to shoot a raycast into the world and see if the touch hit the hand. From there, it’s just like dragging with a mouse - track the position to the touch until it is released, then stop tracking the position to the touch.

I forgot to mention about touch controls on desktop temporarily… you should be able to just add code that would work on desktop and have it work on both.

EDIT: Added potential code solution as requested by question author.

using UnityEngine;

public class TouchTracking : MonoBehaviour {

    [SerializeField] private Transform target;
    [SerializeField] private Camera viewport;
    [SerializeField] private bool useMouse = true;

    // these keep track of important tracking info, such as finger and whether we're tracking or not
    private int trackingFinger = 0;
    private bool track = false;

    void Awake() {
        // if you leave the viewport field blank in the Inspector, 
        // use the first camera in your scene with the "MainCamera" tag.
        if (viewport == null)
            viewport = Camera.main;
    }

    void Update() {
        /**
        * Touch
        */
        int touchCount = Input.touchCount; // this tells you how many fingers are on the screen right now
        Touch touch; // this stores information about the touch
        for (int id = 0; id < touchCount; id++) {
            touch = Input.GetTouch(id);
            if (touch.phase == TouchPhase.Began && CheckCollision(touch.position))
                track = true;
            else if (touch.phase == TouchPhase.Ended)
                track = false;
        }

        /**
        * Mouse
        */
        // button 0 is left mouse
        if (Input.GetMouseButtonDown(0) && CheckCollision(Input.mousePosition))
            track = true;
        else if (Input.GetMouseButtonUp(0))
            track = false;
        /**
        * Perform the tracking operation if needed
        */
        if (track) {
            Vector3 inputPos;
            if (useMouse)
                inputPos = Input.mousePosition;
            else
                inputPos = Input.GetTouch(trackingFinger).position;

            Ray r = viewport.ScreenPointToRay(inputPos);
            float distance = (target.position - r.origin).magnitude;
            MoveToTouch(target, r.GetPoint(distance));
        }
    }

    bool CheckCollision(Vector3 position) {
        Ray ray = viewport.ScreenPointToRay(position);
        Vector3 worldPoint = ray.origin;
        Vector3 worldDirection = ray.direction;

        RaycastHit hit; // stores information about the raycast hit (if there was one)
        if (Physics.Raycast(worldPoint, worldDirection, out hit, Mathf.Infinity))
            return hit.collider.gameObject == target.gameObject;
        else return false;
    }

    void MoveToTouch(Transform target, Vector2 position) {
        float lerpedX = Mathf.Lerp(target.position.x, position.x, 100 * Time.deltaTime);
        float lerpedY = Mathf.Lerp(target.position.y, position.y, 100 * Time.deltaTime);

        target.position = new Vector3(lerpedX, lerpedY, target.position.z);
    }
}

you can use Lean, which can be installed for free in the AssetStore