Hi, I’m currently working an a first person role playing game and at the moment I’m working on an inventory system. I currently have a very basic one thanks to a thread which I can’t seem to find anymore sadly. But there seems to be a problem with it, when I left click an item it drops the item like it’s supposed to… but if I right click the item the item stays in my inventory and drops a clone if the item.
Here is the script:
static var statInventory : Inventory;
//Our inventory
var inventory : Array;
//This will be drawn when a slot is empty
public var emptyTex : Texture;
//the size of the inventory in x and y dimension
public var inventorySizeX = 8;
public var inventorySizeY = 5;
//The pixel size (height and width) of an inventory slot
var iconWidthHeight = 20;
//Space between slots (in x and y)
var spacing = 4;
//set the position of the inventory
public var offSet = Vector2( 100, 100 );
// TEST VARIABLES
// Assign these to test adding Items
public var testTex : Texture;
public var testTex2 : Texture;
public var testInvObject : GameObject;
public var testInvObject2 : GameObject;
//Our Representation of an InventoryItem
@System.Serializable
class InventoryItem
{
//GameObject this item refers to
var worldObject : GameObject;
//What the item will look like in the inventory
var texRepresentation : Texture;
}
// Create the Inventory
function Awake()
{
statInventory = this;
inventory = new Array(inventorySizeX);
for( var i = 0; i < inventory.length; i ++ )
{
inventory[i] = new Array(inventorySizeY);
}
}
function OnGUI()
{
var texToUse : Texture;
var currentInventoryItem : InventoryItem;
//Go through each row
for( var i = 0; i < inventory.length; i ++ )
{
// and each column
for( var k = 0; k < inventory[i].length; k ++ )
{
currentInventoryItem = inventory[i][k];
//if there is an item in the i-th row and the k-th column, draw it
if( inventory[i][k] == null )
{
GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), emptyTex );
}
else
{
GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), currentInventoryItem.texRepresentation );
}
if(currentInventoryItem != null
GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), "", GUIStyle("label") ))
{
currentInventoryItem.worldObject.transform.position = transform.position;
currentInventoryItem.worldObject.transform.rotation = transform.rotation;
currentInventoryItem.worldObject.active = true;
if(Input.GetMouseButtonUp(0))
{
//Equip it
currentInventoryItem.worldObject.transform.parent = transform;
} else if(Input.GetMouseButtonUp(1))
{
//Drop it
inventory[i][k] = null;
currentInventoryItem.worldObject.transform.parent = null;
}
}
}
}
if( GUILayout.Button("AddItem1"))
{
var newInvObj = Instantiate(testInvObject, Vector3.zero, Quaternion.identity);
newInvObj.active = false;
AddItem( newInvObj, testTex );
}
if( GUILayout.Button("AddItem2"))
{
var newInvObj2 = Instantiate(testInvObject2, Vector3.zero, Quaternion.identity);
newInvObj2.active = false;
AddItem( newInvObj2, testTex2 );
}
}
function AddItem( item : InventoryItem )
{
//Go through each row
for( var i = 0; i < inventory.length; i ++ )
{
// and each column
for( var k = 0; k < inventory[i].length; k ++ )
{
//If the position is empty, add the new item and exit the function
if( inventory[i][k] == null )
{
inventory[i][k] = item;
return;
}
}
}
//If we got this far, the inventory is full, do somethign appropriate here
}
function AddItem( worldObject : GameObject, texRep : Texture )
{
var newItem = new InventoryItem();
newItem.worldObject = worldObject;
newItem.texRepresentation = texRep;
AddItem( newItem );
}
and here is the code for the item:
public var iconTex : Texture;
function OnMouseDown()
{
gameObject.active = false;
var item = new InventoryItem();
item.worldObject = gameObject;
item.texRepresentation = iconTex;
Inventory.statInventory.AddItem(item);
}
at the moment I can pick up items by clicking them with the mouse… but it’s very hard to target it since my camera moves with the mouse and I can pickup the item from any distance. dose anyone have any tips or know a way to make a sort of target in the center of the screen (like a crosshair) where if it is over the item and the player is within a certain distance from the item then the player picks up the item.
I’m not very good at scripting, but I’m hoping that if I ask around the unity forums, I will eventually start to understand it more and more.