The C# scripts are a wip for a level I’m working on in Unity.

InventoryItem.cs instantiates or destroys objects moving in or out of the inventory.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

// http://trinary.tech/category/mec/ more efficient coroutines documentation
using MEC;


// Eric's items inventory, controls cards behavior in inventory and instantiates or destroys world object counterparts and interacts directly with ItemsWorld script
public class InventoryItem : MonoBehaviour //, IDragHandler, IBeginDragHandler, IEndDragHandler, IDropHandler
{
    public GameObject worldCounterpart; // use prefab from project tab so that it can be instantiated and destroyed?
    private GameObject worldItem; // cloned copy of the world counterpart

    // rectangle transform of current card to send to inventory
    private RectTransform itemRectTransform;

    // get the canvas raycast
    public GameObject inventoryPanel;
    private Inventory inventory;

    // setup spawn item
    private Vector3 itemSpawnPoint;
    private Rigidbody worldItemRigidbody;
    private Vector3 keySpawnTempVar = new Vector3(-.35f, 0f, -.5f);
    private WorldInteraction worldInteraction; // get world raycast


    private bool dragStarted = false;


    private void Awake()
    {

        inventory = inventoryPanel.GetComponent<Inventory>();

        worldInteraction = Camera.main.GetComponent<WorldInteraction>();


    }

    private void Start()
    {
        //Debug.Log(deck.name);
    }

    private void Update()
    {

    }

    #region Eric's custom MEC item drag

    public void StartItemDrag(GameObject item) // this method is called from interaction.cs and Inventory.cs
    {
        // start proper raycaster, set card raycast block, start itemdrag with
        GetComponent<CanvasGroup>().blocksRaycasts = false;
        if (!dragStarted) Timing.RunCoroutine(_itemDrag(item));

    }

    IEnumerator<float> _itemDrag(GameObject item)
    {
        // itemsInWorld.inventoryCounterpart.transform.position = Input.mousePosition;
        dragStarted = true;

        while (!Input.GetMouseButtonUp(0))
        {
            //Debug.Log("Drag started");

            item.transform.position = Input.mousePosition; // transform or rectTransform?            

            yield return 0;
        }

        #region coroutine ending - sort card, spawn item, and/or send item to required item check
        // everything in this region related to setting position of world item and instantiating it may be better off in world interaction script?

        itemRectTransform = item.GetComponent<RectTransform>();

        if (inventory.lastRaycastHit != null) // still need to differentiate between items and collectibles inventory
        {
            // send card to hand
            inventory.InHand(this.gameObject, itemRectTransform);

        }
        else
        {
            // send card to deck
            inventory.InDeck(this.gameObject, itemRectTransform);


            // set spawnpoint for world counterpart
            if (!worldInteraction.hitDetected) // raycast isn't hitting something
            {
                itemSpawnPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, (Camera.main.nearClipPlane + .6f))); // added .6f for approx 2 feet
                InstantiateWorldItem();
            }
            else if (worldInteraction.hitDetected && worldInteraction.worldItem != null && worldInteraction.worldItem.usable)
            { /*use the interactable object*/
                Debug.Log("Item has been dropped on usable item");

                // spawn object, set parent, set position

                // extend itemSpawnPoint later to grab the point on the new parent world item where this item should be located,
                // maybe add empty game objects called spawn points (or name spawnKey, spawnPowerCell etc) to the objects to set the position to or
                itemSpawnPoint = Vector3.zero;
                InstantiateWorldItem(worldInteraction.worldItem.transform);

                // temporary key send
                worldInteraction.worldItem.UnlockDoor(this.gameObject);
            }
            else
            {
                // get normal and adjust itemspawnpoint a few inches from it                
                itemSpawnPoint = (worldInteraction.raycastHit.normal * .127f) + worldInteraction.raycastHit.point; // .127f is five inches
                InstantiateWorldItem();
            }


        }
        #endregion


        GetComponent<CanvasGroup>().blocksRaycasts = true;
        dragStarted = false;
        //Debug.Log("done dragging card");
    }


    private void InstantiateWorldItem()
    {
        // instantiate world counterpart item
        worldItem = Instantiate(worldCounterpart, itemSpawnPoint, Quaternion.identity); // Input.mousePosition

        // set card as inventory counterpart in world item
        worldItem.GetComponent<WorldItem>().inventoryCounterpart = this.gameObject;// also temporary?
    }

    // my first overload
    private void InstantiateWorldItem(Transform newParent)
    {
        InstantiateWorldItem();

        worldItemRigidbody = worldItem.GetComponent<Rigidbody>(); // should be one so shouldn't need null check as all pickable items should be rigid bodies?
        worldItemRigidbody.isKinematic = true;
        worldItemRigidbody.detectCollisions = false; // can't pick up key again unless remove this line and set all collision shapes to trigger

        // there's a huge difference between worldInteraction.worldItem.etc vs worldItem.etc...one the world raycast just hit and one the item being spawned
        worldItem.transform.SetParent(newParent);
        worldItem.transform.localPosition = keySpawnTempVar;
    }

    #endregion

}


</center>