Hi all, I have been stuck with my game for the past two days wondering exactly how to implement a feature. (If its worth knowing this is my first game and I’ve used Brackey’s “How to make a 3D RPG” YouTube series to make it.
The issue I have is when the character equips an item from their backpack (Inventory). I want to make the Object appear attached to the player’s hand. All of this is done by two scripts “ItemPickup” and “GearManager”. ItemPickup essentially allows the player to click on an object in the game and if that object is an item, the objects’ SetActive is changed to false. That item is also added to an inventory. My GearManager scripts deals with everything such as creating the slots and spaces for the inventory as well as having the function to equip or unequip an item. This all works, but no graphic is shown in game and the player doesn’t actually hold the equipped weapon. If anyone has any suggestion I would greatly appreciate it! I apologise for my bad code too, I have changed things for days trying to get things working so it is messy.
here is the GearManager Script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GearManager : MonoBehaviour
{
#region Singleton
public static GearManager instance;
public GameObject Hand;
Vector3 newPosition;
Vector3 newRotation;
void Awake()
{
instance = this;
}
#endregion
Gear[] currentGear;
public delegate void OnGearChanged(Gear newItem, Gear oldItem);
public OnGearChanged onGearChanged;
Inventory backpack;
void Start()
{
backpack = Inventory.instance;
int slotsNumber = System.Enum.GetNames(typeof(GearSlot)).Length;
currentGear = new Gear[slotsNumber];
}
public void ItemAxis(Vector3 position, Vector3 rotation)
{
newPosition = position;
newRotation = rotation;
}
public void Equip (Gear newItem)
{
int slotIndex = (int)newItem.gearSlot;
Gear oldItem = Unequip(slotIndex);
if (onGearChanged != null)
{
onGearChanged.Invoke(newItem, oldItem);
}
currentGear[slotIndex] = newItem;
//Make the model that was hidden in ItemPickup active again and transform it into players hand
}
public Gear Unequip (int slotIndex)
{
if (currentGear[slotIndex] != null)
{
Gear oldItem = currentGear[slotIndex];
backpack.Add(oldItem);
currentGear[slotIndex] = null;
if (onGearChanged != null)
{
onGearChanged.Invoke(null, oldItem);
}
return oldItem;
}
return null;
}
ItemPickup:
using UnityEngine;
public class ItemPickup : Interactable
{
public string Name;
public GameObject EquipSlot;
public Vector3 PickPosition;
public Vector3 PickRotation;
public Item item;
public override void Interact()
{
base.Interact();
PickUp();
}
void PickUp()
{
bool ifPickedUp = Inventory.instance.Add(item);
if (ifPickedUp)
gameObject.SetActive(false);
}
}
The solution I have tried to come up with was the following code placed inside ItemPickup:
public void EquipItemOnPlayer()
{
gameObject.SetActive(true);
gameObject.transform.parent = EquipSlot.transform;
gameObject.transform.localPosition = PickPosition;
gameObject.transform.localEulerAngles = PickRotation;
}
public void UnequipItemOnPlayer()
{
gameObject.SetActive(false);
}
I then tried to call those methods in “GearMananger” under equip and unequip respectively. It worked but I had to reference a game object in GearManager using the GameObject.FindGameObjectsWithTag(“Item”). This however just showed the first Item it found in the list. (Essentially it showed a model of Iron_Axe instead of and Iron_Sword due to alphabetical order.
Again I am sorry for the read, my explanation of the issue is probably just as bad. I just want to find the gameobject that is associate with the item in my inventory, and attach it to my players hand when its equipped so you can see the item. Thanks in advance.