- Home /
Help attaching object to players hand
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.
Hey @alexconian What is data structure for your item? what I mean is that is it MonoBehaviour ? if you send your item script I can help you with that.
Answer by PhotonPotatoDev · Apr 27, 2021 at 11:24 PM
Hey @alexconian, I would try making a point (empty gameObject) that follows the player model's hand (basically a child of the hand). Then make whatever item you want to appear got to that position and rotation possibly with an offset.
Here's just a few untested example lines that I wrote:
Transform tempTransform = this.gameObject.GetComponent<Transform>();
tempTransform.position = playerHandTransform.position + /*assign a public Vector3 variable so that you can change the offset of the hand item from the player's hand*/;
tempTransform.rotation = playerHandTransform.rotation + /*insert a public Quaternion variable to create an offset like the line of code above*/;
I really hope that this helps you!
EDIT This script requires that it is attached to an individual gameObject that has the model of the item that you want to attach to the players hand. Also, you will need a public Transform that has to be set to the Transform of the empty gameObject I was talking about earlier (the one that follows the player's hand)
I have actually got it working with something like this! Thank you very much, can finally get on with it now. Thanks again!
Your answer
Follow this Question
Related Questions
Could not start compilation, Win32Exception 1 Answer
Player dies when it Hits/crushes a block 2 Answers
Unity error code cs0101 already contains a definition for movement? 0 Answers
Create 3d hexagonal terrain 1 Answer
Movement Code not working 2 Answers