- Home /
moving a object from ground to inventory
Hi I made a inventory with 28 slots and each slot has box collider named "inventory slot1" and it goes on like that tell it hits the 28th.
I need it so when i pick up a item it goes to 1st empty slot in my inventory and have option to drop the item from the inventory to be in front of the character.
If you can include a pick-up script it help out a lot.
thankyou
But what do you have so far? Before answering do consider that if you have nothing you have a big chance of getting nothing back.
I have a inventory image and the image is attached to a plane and the box colliders are also i also have a pick-up image with box colliders over the options thats all I have so far i tried scripting it myself but i was never sure on how to word the script in order to make it work i know i need to make false statements and true statements and i also need a var for each variable "option" but that is all i know when it comes to it sorry that I don't have premade script but not 100% sure how to make my own yet
var pick-up
var full
void On$$anonymous$$ouseEnter()
{
animation.CrossFade("log options");
}
if BoxCollider "pick-up"
if true reset position = BoxCollider "inventory slot1"
if BoxCollider "inventory slot1" = full == false
if BoxCollider "inventory slot1" = false reset position = "inventory slot2"
if true reset position = BoxCollider "inventory slot2"
if BoxCollider "inventory slot2" = full == false
if BoxCollider "inventory slot2" = false reset position = "inventory slot3"
if true reset position = BoxCollider "inventory slot3"
if BoxCollider "inventory slot3" = full == false
if BoxCollider "inventory slot3" = false reset position = "inventory slot4"
if true reset position = BoxCollider "inventory slot4"
if BoxCollider "inventory slot4" = full == false
if BoxCollider "inventory slot4" = false reset position = "inventory slot5"
if true reset position = BoxCollider "inventory slot5"
if BoxCollider "inventory slot5" = full == false
if BoxCollider "inventory slot5" = false reset position = "inventory slot6"
if BoxCollider "inventory slot6" = full == false
{
animation.CrossFade("log options");
}
}
so this far as i got but i know i did something wrong i dont know how to script 100% yet as i said before
Answer by cdrandin · Aug 02, 2013 at 09:57 PM
OK, an inventory is simply a collection of items. These items are of some data type, for simplicity thing of these as integers.
0 - Sword 1 - Bow 2 - Health_Potion
Assign these numbers to an object. How? Make a script that describes the objects behavior to act like an item. The script called "Item". Now, how can we describe what the behavior of the script is or what it even is? Let's assign it the numbers we defined previously.
So, this object "Cube" has now have a script "Item" attached to it, given the number 0, meaning this item is our "Sword".
Ok, we have no defined our items. Now, we need some way to store this in our inventory. Simply this can be done by a list. This list is meant to store our items, so it makes sense the list is of data type "Items". List inventory = new List
Now then we have our inventory created. How about adding stuff. Ok, when do we want to do this? Lets say we do this when the player presses the 'F' key and is within 1meter of the object.
Now then. We need to know how to get our items in the first place that is in the scene. Let's cache all our items. AllItems[] = GameObject.FindGameObjectsWithTag()
This will store all of our "Items" type object into an array.
So, we need to traverse through this array and check if the object is within distance of our player AND if the player so happens to press the 'F' key we pick up the object. Simply this can be done with inventory.add(item)
Note this item is different from our "Items" type. Now then at that point you have 2 choices. You can either destroy the object or disable the renderer of it and move it far our of reach. Why these 2 option depends if you want to reuse the item again faster, because Instantiate and Destroy are expensive calls.
There is no code sorry, but I feel that the general idea is more important and the code is up to the person who wants this to work.
Improvements Possibly, don't always check through arrays. Put collision on your items and only search when there is a trigger between the item and your player. Your items can have more functionality. Don't need to restrict to the number convention I have. I have recently created my own inventory and works for how I want it. Still could be better. You could make a very generalize "Items" then have another "ItemInit" class which keeps track of multiple, same name objects. To help organize. SO, for most games, Health potions are "stackable" so instead of have 5 slots for the 5 potions, you could have 1 slot with 5 potions available.
ok so lets say when i move the mouse over the object i want it to display the options to pick it up ins$$anonymous$$d of having to be with in distance of the object how do you recommend i go about doing that?
This could be achieved using raycasting. Unity provides Screen to World functionality. http://docs.unity3d.com/Documentation/ScriptReference/Camera.ViewportToWorldPoint.html
Not sure if this is the right one. What you want to achieve is make sure to point a ray out towards the view port into world space and if the ray collides with our "Items" object then you know you can click.
Also, you will need distance no matter what, because you can point a ray cast out 100m and if you hit an "Items" object far away, without distance you can pick up an item from any distance.
well i tried to make the script but honestly i was 100% lost while trying do you think you can type the code that i can mess with i dont need full thing just a example to go off on