- Home /
Question by
kobatonguyen · Aug 04, 2018 at 04:47 PM ·
c#scripting problem
How can i limit object dragging area in my inventory?
I want to limit the dragging area of the object to the size of inventory panel. If I drag the object out of the inventory, It would become a prefab.
This is my code for dragging as well as prefab while dragging the item. My code turns the object immediately into prefab when I drag it and I don't know how to fix it. Thank you:
public void OnDrag(PointerEventData eventData)
{
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = objPosition;
Transform item_prefab = itemBeingDragged.GetComponent<InventoryItem>().item_prefab;
item_prefab.position = mousePosition;
// Instantiate it to world position
Instantiate(item_prefab, objPosition, new Quaternion());
Destroy(itemBeingDragged);
}
I want to make an inventory like this example. Example: -Pic1: The object is draggable everywhere in the inventory. - Pic 2: It turns into prefab when we drag the object out of the inventory.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612172209im_/https://answers.unity.com/themes/thub/images/avi.jpg)