- Home /
Question by
Mistyisty123 · Aug 01, 2019 at 07:25 PM ·
raycastinventoryraycasthitaddhit detection
Raycast is finding all items/script is finding all items - inventory help
Hello,
I have an inventory, and trying to do picking up items using Raycast! It all works fine, however it finds all the "items", and adds them to the inventory not the hitInfo item, what would be the solution for this, so that it only adds the item I'm looking at and not all of them?
The code is below:
using UnityEngine;
using System.Collections;
public class PickUpItem : MonoBehaviour
{
public Item item;
private Inventory _inventory;
private GameObject _player;
// Use this for initialization
private Camera mainCamera;
private Interact interact;
void Start()
{
mainCamera = Camera.main;
interact = GameObject.FindGameObjectWithTag("Player").GetComponent<Interact>();
_player = GameObject.FindGameObjectWithTag("Player");
if (_player != null)
_inventory = _player.GetComponent<PlayerInventory>().inventory.GetComponent<Inventory>();
}
// Update is called once per frame
void Update()
{
Ray ray = mainCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo, interact.interactRange))
{
if (_inventory != null && Input.GetKeyDown(KeyCode.E) && hitInfo.collider.tag == "FoodItem")
{
bool check = _inventory.checkIfItemAllreadyExist(item.itemID, item.itemValue);
if (check)
Destroy(hitInfo.collider.gameObject);
else if (_inventory.ItemsInInventory.Count < (_inventory.width * _inventory.height))
{
_inventory.addItemToInventory(item.itemID, item.itemValue);
_inventory.updateItemList();
_inventory.stackableSettings();
Destroy(hitInfo.collider.gameObject);
}
}
}
}
}
Comment
You have to provide your code for your inventory in order to answer this question. :-)
Also in your Raycast's "if statement" it doesn't seem like you ever set the item to anything new, which probably also is causing you problems.
Answer by niiicolai · Aug 01, 2019 at 09:02 PM
Hi @Mistyisty123 You could do something like this:
using UnityEngine;
using System.Collections;
public class PickUpItem : MonoBehaviour {
public string title;
public int amount;
}
using UnityEngine;
using System.Collections;
public class InventoryController : MonoBehaviour {
[SerializeField] private itemTag = "Item"; // Remember to setup your tag, aswell on the gameobjects.
private List<string> items;
private List<int> amounts;
[SerializeField] private KeyCode interactKey;
[SerializeField] private float interactRange;
private RaycastHit hitInfo;
private void Start() {
items = new List<string>();
amounts = new List<int>();
}
private void Update() {
if (Input.GetKeyDown(interactKey) {
Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
if (Physics.Raycast(ray, out hitInfo, interactRange)) {
if (hitInfo.CompareTag(itemTag)) {
var item = hitInfo.collider.GetComponent<PickUpItem>();
AddItem(item.title, item.amount);
Destroy(item.gameObject);
}
}
}
}
public void AddItem(string itemTitle, int amount) {
var exist = false;
for (int i = 0; i < items.Count; i++) {
if (items[i] == itemTitle) {
amounts[i] += amount;
exist = true;
break; // Remember to break the loop
}
if (!exist) {
items.Add(itemTitle);
amounts.Add(amount);
}
}
public void RemoveItem(string itemTitle, int amount) {
for (int i = 0; i < items.Count; i++) {
if (items[i] == itemTitle) {
amounts[i] -= amount;
if (amounts[i] <= 0) {
items.RemoveAt(i);
amounts.RemoveAt(i);
}
break; // Remember to break the loop
}
}
}
// The inventory script above saves the item title and amount.
// If you later want to get that item out as a GameObject you would need to do add
// something like this to the inventory controller.
[SerializeField] private PickUpItem[] itemPrefabs;
private PickUpItem GetItemBy(string title) {
PickUpItem item = null;
for (int i = 0; i < itemPrefabs.Length; i++) {
if (itemPrefabs[i].title == title) {
item = itemPrefabs[i];
break;
}
}
return item;
}
// When you have a method to get the prefab you can extend your inventory with a drop method
[SerializeField] private Transform dropPoint;
private void DropItem(string title, int amount) {
var canDrop = false;
for (int i = 0; i < items.Count; i++) {
if (items[i] == title) {
canDrop = (amount <= amounts[i]);
break;
}
}
if (canDrop) {
var prefab = GetItemBy(title);
var clone = Instantiate(prefab);
clone.transform.position = dropPoint.position;
clone.amount = amount;
RemoveItem(title, amount); // Remember to remove the items from your inventory
}
}
}