Okay, so I’ve been designing an inventory system for the game I’m working on. Let’s begin with the code I’ve got working.
var InventoryFrame : Texture;
var GunFrame : Texture;
var InventoryXPos = 650;
var InventoryYPos = 150;
var OpenInventory : boolean = false;
var InventorySlot01 : Texture;
var InventorySlot02 : Texture;
var InventorySlot03 : Texture;
var InventorySlot04 : Texture;
var InventorySlot05 : Texture;
var InventorySlot06 : Texture;
var InventorySlot07 : Texture;
var InventorySlot08 : Texture;
var InventorySlot09 : Texture;
var isControllable : boolean = true;
function OnGUI(){
GUI.Label (Rect(20,20, 256, 113), GunFrame);
if (OpenInventory == true){
GUI.Label(Rect(InventoryXPos,InventoryYPos, 256, 404), InventoryFrame);
GUI.Label(Rect(InventoryXPos + 10, InventoryYPos + 18, 80, 80), InventorySlot01);
GUI.Label(Rect(InventoryXPos + 92, InventoryYPos + 18, 80, 80), InventorySlot02);
GUI.Label(Rect(InventoryXPos + 174, InventoryYPos + 18, 80, 80), InventorySlot03);
GUI.Label(Rect(InventoryXPos + 10, InventoryYPos + 105, 80, 80), InventorySlot04);
GUI.Label(Rect(InventoryXPos + 92, InventoryYPos + 105, 80, 80), InventorySlot05);
GUI.Label(Rect(InventoryXPos + 174, InventoryYPos + 105, 80, 80), InventorySlot06);
GUI.Label(Rect(InventoryXPos + 10, InventoryYPos + 189, 80, 80), InventorySlot07);
GUI.Label(Rect(InventoryXPos + 92, InventoryYPos + 189, 80, 80), InventorySlot08);
GUI.Label(Rect(InventoryXPos + 174, InventoryYPos + 189, 80, 80), InventorySlot09);
}
}
function Update()
{
if (Input.GetKeyDown ("tab")){
if (OpenInventory == true){
//if the Inventory is already open, close it when the tab button is pressed
OpenInventory = false;
Screen.lockCursor = true;
isControllable = true;
}
else if(OpenInventory == false){
//if the inventory is closed, open it when the tab button is pressed
OpenInventory = true;
Screen.lockCursor = false;
Screen.showCursor = true;
isControllable = false;
}
}
The code above controls the inventory GUI. It’s a grid of 9 squares, each representing the items in your inventory. It is shown whenever the Tab key is pressed.
Now where I’m running into issues is here:
function Update(){
var ThePlayer;
var Facing = transform.TransformDirection(Vector3.forward);
var ObjectHit : RaycastHit;
if(Physics.Raycast(transform.position, Facing, ObjectHit, 2.0)){
}
}
This is the script that basically translates the 3d object from 3d into a 2d GUI item. Basically, what I’m trying to do, is have the raycast hit a collider, then I need to be able to check if that collider belongs to an Item that can be picked up, then destroy that object in the 3d world, and add the icon to the inventory.
How do I go about doing this? Any help would be appreciated