C# holding item script

Hi guys, i got this script which give me possibility to move item in front of my character - but i have two problems, hope you can help me with it :slight_smile:
First, when im holding item, it can go through walls and other item, how can i fix this?
Seond, can i make some improvment to increase distance when character can grab item?
Sorry for english by the way :wink:

And here is the code (very simple)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class draggableitem : MonoBehaviour {

Vector3 dist;
float posX;
float posY;

void OnMouseDown()
{
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;

}
void OnMouseDrag()
{
Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, dist.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
}

Check the first or second post for code formatting help.

One way to keep your object from entering other objects is to use physics and some type of joint (probably a fixed joint?) to attach it to the player, and then prohibit them from entering actual geometry. Obviously you need a Rigidbody and collider (or at least a proxy) on the held item to make this work.