Picked up objects passes through objects

As the title says when ever i pick up an object in my game the picked up item phases through other objects such as the walls and floor. How can I stop this?

using UnityEngine;
using System.Collections;

public class GrabAndDrop : MonoBehaviour {
    GameObject grabbedObject;
    float grabbedObjectSize;
	
	
    GameObject GetMouseHoverObject(float range)
    {
        Vector3 postion = gameObject.transform.position;
        RaycastHit raycastHit;
        Vector3 target = postion + Camera.main.transform.forward * range;
        if (Physics.Linecast(postion, target, out raycastHit))
            return raycastHit.collider.gameObject;
        return null;
    }

    void TryGrabObject(GameObject grabObject)
    {
        if (grabObject == null || !CanGrab(grabObject))
            return;
        grabbedObject = grabObject;
        grabbedObjectSize = grabObject.GetComponent<Renderer>().bounds.size.magnitude;

    }
    
    bool CanGrab(GameObject candidate)
    {
        return candidate.GetComponent<Rigidbody>() != null;
    }
	
    void DropObject()
    {
        if (grabbedObject == null)
            return;
        if(grabbedObject.GetComponent<Rigidbody>()!= null)
           grabbedObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
        grabbedObject = null;
    }
    void Update() {
        if (Input.GetMouseButtonDown(1))
        {
            if (grabbedObject == null)
                TryGrabObject(GetMouseHoverObject(5));
            else
                DropObject();
        }
   
        if (grabbedObject != null)
        {
            Vector3 newPosition = gameObject.transform.position+Camera.main.transform.forward * grabbedObjectSize;
            grabbedObject.transform.position = newPosition;
        }	
	}
}

Thanks in advance for anyone who could help me.

PS. I just started learning C# by following tutorials so an easy explanation would be nice.

I assume you expect the object to stop at walls due to having the rigidbody?

Unfortunately your script works against the physics engine. You set the objects position directly. Even though the phyics engine tries to work against it (trying to apply collision forces) you’ll end up what’s being applied as position before the frame is rendered to the screen.

If you want your object to reliably interact with static colliders, the only good way to do that is to NOT use either of “Transform.position” or “Rigidbody.MovePosition()”.

Most likely, the best course of action would be make the object float in front of you by applying physics forces (Rigidbody.AddForce()), but then you run the risk of the held object being capable of smashing other physics objects out of the way with extraordinary force (although, that would potentially happen to a greater degree with MovePosition).