-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraggableitem.cs
More file actions
67 lines (58 loc) · 1.9 KB
/
draggableitem.cs
File metadata and controls
67 lines (58 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using UnityEngine;
using UnityEngine.EventSystems;
namespace InvScripts {
public class DraggableItem2 : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private Transform initialParent;
private Vector3 initialPosition;
public Vector3 fixedSize = new Vector3(1.0f, 1.0f, 1.0f);
private bool hasBeenDropped = false;
private bool condition2 = false;
public Camera cam;
public void OnBeginDrag(PointerEventData eventData)
{
initialParent = transform.parent;
initialPosition = transform.position;
transform.SetParent(transform.parent.parent);
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
Vector3 newPosition = cam.ScreenToWorldPoint(Input.mousePosition);
newPosition.z = initialPosition.z; // Maintain the initial Z-axis position
transform.position = newPosition;
}
public void OnEndDrag(PointerEventData eventData)
{
transform.SetParent(initialParent);
if (transform.parent.GetComponent<InventorySlot>() != null)
{
transform.position = initialPosition;
if (!hasBeenDropped)
{
transform.localScale = fixedSize; // Set to the fixed size only once
Debug.Log("ur code trap");
hasBeenDropped = true;
}
GetComponent<CanvasGroup>().blocksRaycasts = true;
}
else if (condition2)
{
if (transform.parent.gameObject.tag == "Inventory Slot")
{
condition2 = true;
}
transform.SetAsLastSibling();
}
else
{
Destroy(gameObject);
}
}
public void SetParent(Transform parent)
{
transform.SetParent(parent);
transform.localPosition = Vector3.zero;
}
}
}