Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alexconian · Apr 27, 2021 at 08:26 PM · scripting problem3d

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fariborzzn · Apr 27, 2021 at 09:44 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexconian · Apr 28, 2021 at 09:21 AM 0
Share

I have actually got it working with something like this! Thank you very much, can finally get on with it now. Thanks again!

avatar image PhotonPotatoDev alexconian · Apr 28, 2021 at 07:53 PM 0
Share

No problem!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

235 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges