private void DropItem(Item item) { GameObject prefab = item.OverridePrefab != null ? item.OverridePrefab : item.Prefab; float angle = Random.Range(0f, 360f); float x = (float)(InventoryManager.DefaultSettings.maxDropDistance * Mathf.Cos(angle * Mathf.PI / 180f)) + gameObject.transform.position.x; float z = (float)(InventoryManager.DefaultSettings.maxDropDistance * Mathf.Sin(angle * Mathf.PI / 180f)) + gameObject.transform.position.z; Vector3 position = new Vector3(x, 1f, z); GameObject go = InventoryManager.Instantiate(prefab, position, Random.rotation); ItemCollection collection = go.GetComponent <ItemCollection>(); if (collection != null) { collection.Clear(); collection.Add(item); } }
//Try to drop the item to ground private void DropItem() { if (Container.IsLocked) { InventoryManager.Notifications.inUse.Show(); return; } if (ObservedItem.IsInCooldown) { return; } //Get the item to drop Item item = dragObject != null ? dragObject.item : ObservedItem; //Check if the item is droppable if (item != null && item.IsDroppable) { //Get item prefab GameObject prefab = item.OverridePrefab != null ? item.OverridePrefab : item.Prefab; RaycastHit hit; Vector3 position = Vector3.zero; Vector3 forward = Vector3.zero; if (InventoryManager.current.PlayerInfo.transform != null) { position = InventoryManager.current.PlayerInfo.transform.position; forward = InventoryManager.current.PlayerInfo.transform.forward; } //Cast a ray from mouse postion to ground if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit) && !UnityTools.IsPointerOverUI()) { //Clamp the drop distance to max drop distance defined in setting. Vector3 worldPos = hit.point; Vector3 diff = worldPos - position; float distance = diff.magnitude; //if player is null this does not work! if (distance > (InventoryManager.DefaultSettings.maxDropDistance - (transform.localScale.x / 2))) { position = position + (diff / distance) * InventoryManager.DefaultSettings.maxDropDistance; } else { position = worldPos; } } else { position = position + forward; } //Instantiate the prefab at position GameObject go = InventoryManager.Instantiate(prefab, position + Vector3.up * 0.3f, Quaternion.identity); go.name = go.name.Replace("(Clone)", ""); //Reset the item collection of the prefab with this item ItemCollection collection = go.GetComponent <ItemCollection>(); if (collection != null) { collection.Clear(); collection.Add(item); } PlaceItem placeItem = go.GetComponentInChildren <PlaceItem>(true); if (placeItem != null) { placeItem.enabled = true; } ItemContainer.RemoveItemCompletely(item); Container.NotifyDropItem(item, go); } }