JensHendar.com

Inventing an inventory in Unity3D (Part 2)

Since I made the last post (yesterday…) I’ve rewritten some of the code for the inventory to allow saving/loading, but in this post I will continue with the code we hade in the last post. If you haven’t read that yet I suggest you do before continueing.

shingle_throw

When we ended the last part we had successfully added an object to our inventory. What we want to do now is set an equipped object when we press our number buttons. So we do a getKeyDown for the number keys:

[code lang=”js”]
function Update() {
if (Input.GetKeyDown(“1”)) {
setEquippedObject(1);
}
}

function setEquippedObject(equippedItemNumber : int) {
equippedObjectNumber=equippedItemNumber;
if (equippedObject) { equippedObject.SetActive(false); }
var tempItem : UnityEngine.GameObject;
tempItem = inventoryList[(equippedItemNumber-1)] as UnityEngine.GameObject;
if (tempItem.name != “Empty”) {
tempItem.gameObject.SetActive(true);
equippedObject = tempItem;
equippedObjectText.GetComponent(UI.Text).text = equippedObject.name;
equippedObjectAction.SetActive(true);
} else {
equippedObjectText.GetComponent(UI.Text).text = “”;
equippedObjectAction.SetActive(false);
}
}

function updateItemTag() {
currentItem.gameObject.tag = “Moving”;
currentItem=null;
}
[/code]

What we basically do in the setEquippedObject-function (which takes the number of the key as a parameter) is to:

  • If there already is an equippedObject, set it to null.
  • Check our inventoryList for the item with the corresponding number.
  • Enable the item.
  • Show it’s name on the screen together with an “action text”.

And if there isn’t an object in that slot we just texts.

Now if we want to do something with the object when the player presses “f”, we do this:

[code lang=”js”]
function Update() {
if (Input.GetKeyDown(“f”)) {
doItemAction(“throw”);
}

}

function doItemAction(action : String) {

switch (action) {
case “throw”:
if (equipItemObject.transform.childCount > 0) {
currentItem = transform.gameObject.GetComponent(CharacterInventory).equippedObject.transform;
currentItem.gameObject.AddComponent(Rigidbody);
currentItem.gameObject.GetComponent(Rigidbody).collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
currentItem.transform.parent = objectsParent.transform;
currentItem.gameObject.GetComponent(Rigidbody).AddForce(currentItem.transform.forward * 300f);
Invoke(“updateItemTag”, 0.1);
transform.gameObject.GetComponent(CharacterInventory).removeInventoryItem();
}
break;
}
}

[/code]

And what we do here is:

  • Check to see that there is anything in our inventory (equipItemObject.transform.childCount > 0).
  • Set the MeshCollider to convex (needed for the rigidbody).
  • Add Rigidbody.
  • Set the collisionDetectionMode to ContinousDynamic (to keep it from going through stuff).
  • Set the parent of the object to a “world wide” object (to keep it organised).
  • Add some force (the actual throwing).
  • Run a function that removes the item from inventory (I’ll get to that one).
  • Wait 0.1, then set the tag to “moving” and set the currentItem to null.

The removeInventoryItem-function looks like this:

[code lang=”js”]
function removeInventoryItem () {

inventoryList[equippedObjectNumber-1] = emptyObject;
var i=0;
equippedObjectText.GetComponent(UI.Text).text = “”;
equippedObjectAction.SetActive(false);

for (var child: UnityEngine.Transform in inventoryUI.transform) {
var tempItem : UnityEngine.GameObject = inventoryList[i] as UnityEngine.GameObject;
child.name = tempItem.GetComponent(Item).itemNameInInventory;
child.GetComponent(UI.Image).sprite = tempItem.GetComponent(Item).itemImageInInventory;
i++;
}
equippedObject = null;
}
[/code]

And does this:

  • Sets the old object to empty.
  • Sets the item text/item action text to null.
  • Iterates through the children of the Inventory-object and ‘resets’ them all.
  • Sets the equippedObject-variable to null.

And there we have it. An (probably inefficient) inventory system with item actions.

I know that this might be hard to follow, but I’m just getting started with these posts and hopefully I’ll get better over time.

Have a good one!

Next Post

Previous Post

© 2024 JensHendar.com

Theme by Anders Norén