Grab object and check collision

I want to make a grab object script. Here is my code:

    bool isDrag;
    RaycastHit hit;
    GameObject target;
    Ray ray;
    float dist;

    void Update()
    {
        ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
        if (!isDrag)
        {
            if (Physics.Raycast(ray.origin, ray.direction, out hit, 3))
            {
                if (target != null && target != hit.collider.gameObject) target.renderer.material.shader = Shader.Find("Diffuse");
                if (hit.collider.gameObject.tag == "Ball")
                {
                    Debug.Log(hit.collider.gameObject.tag);
                    dist = Vector3.Distance(hit.point, transform.position);
                    target = hit.collider.gameObject;
                    target.renderer.material.shader = Shader.Find("Outlined/Silhouetted Diffuse");
                    if (Input.GetMouseButtonDown(0))
                    {
                        target.GetComponent<Rigidbody>().isKinematic = true;
                        isDrag = true;
                    }
                }
            }
        }
        else
        {
            target.transform.position = transform.position + (ray.direction * dist);
            if (Input.GetMouseButtonUp(0))
            {
                target.GetComponent<Rigidbody>().isKinematic = false;
                isDrag = false;
            }
        }
    }

My problem is if I grab and move object I can move it through other objects like wall. How modify the code? I tried to use another ray to check collision back of the grabbed object but it isn’t worked. Any idea?

Edit:
I use FPS controller from standard assets.

The problem is with line 31:

target.transform.position = transform.position + (ray.direction * dist);

This sets the position independent of the physics engine. So I suggest instead of setting position, you add velocity equal to the velocity of the gameObject this script is attached to.

target.rigidbody.velocity = rigidbody.velocity;

Instead of changing isKinematic of the target gameObject, switch off gravity and constantForce component.

I found a script called “DragRigidBody” in Standard Assets. This script solved my problem.