Mouse move question

I have a script to move my player with the position of the mouse. How can I make it stop if it hits a collider?

var depth = 10.0;
 
function Start ()
{
     Screen.showCursor = false;
}
 
function Update ()
{
     var mousePos = Input.mousePosition;
     var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
     transform.position = wantedPos;
}

Use function OnCollisionEnter(collision : Collision) function in your script. It will be called provided that you have a collider on your gameobject as well as a rigidbody component attached to it.