Unity attach object to player tutorial: How to wield a weapon

Share

Creating the scripts to attach the object to the player

Next step in this unity attach object to player tutorial is to write the C# scripts to make the player wielding the weapon as he approaches it.

  1. Select the Player prefab and change the Tag field in the Inspector panel to the Player entry.
  2. In the Scripts folder create a new C# script named WieldWeapon. Open it in the code editor. Paste the following C# code  :
    [snippet id=’118′]
  3. Add the WieldWeapon  script as a component of the Sword object in the scene, and press the Play button in unity editor to see how the player will pickup the sword once near enough.

In this unity attach object to player tutorial we used a simple way to bind the sword model to the player right hand bone. First we identify the player and its hand bone where we want to attach the object with the Gameobject.Find method. Then we check every frame the distance between the object and the player and when they are close enough we attach the sword to the player’s hand by parenting the Sword transform component to the player’s hand bone transform component. The last steps in the code to attach the object to the player’s hand are resetting the rotation of the sword and then rotate it accordingly on how we want the character to wield the sword. You can see a video of the results of this unity attach object to player tutorial on Gameobject’s Youtube channel.

Download the full unity attach object to player tutorial project code

You can download the unity project file to import the completed project by clockin on the following link :

Unity-attach-object-tutorial-csharp-full-project-download unitypackage full code project download link.

9 thoughts on “Unity attach object to player tutorial: How to wield a weapon”

  1. Hey,

    Thanks for this!
    But, I am having a problem
    NullReferenceException: Object reference not set to an instance of an object
    Weapons.Update () (at Assets/Weapons.cs:22)

    I dragged my player to the player in the inspector and I get this error when I walk close to the weapon.
    This is my path for my hand:
    hand = GameObject.Find(“Player/Minecraft_Rig/Minecraft_Rig|root/Minecraft_Rig|ik_hand.R”);

    Any ideas??

    Thanks!

    Reply
    • Hi !
      First make sure your model has a bone called exactly as the path you assign to hand. I see a “|” in your code which i believe could lead to errors.
      Then if are using a recent Unity version i should try to replicate this tutorial with the latest version and see if it’s still working.

      Reply
  2. anyway you could help with equipping different weapons? after pick up switch between collected weapons. been trying to work it out but no luck so far. Thanks in advance!

    Reply
    • Hi, i am a little busy right now so i have been far from Unity. My ultimate goal is to write a full rpg-like inventory tutorial and cycling trough available weapons will be adressed. It should be possible to write a quick hack with an array or something to make it work, if i find a simple solution i will include it in an update.

      Reply
        • I think that if you are doing an action game you can hardcode the weapon rotation without a complex inventory system.
          Just store the collected weapons gameobjects in an array then instantiate the one you want equipped in the scene and bind it to the hand bone as you’ve seen in this tutorial. Remeber to deparent the previously equipped weapon and destroy its gameobject to make sure you have one weapon in hand at a time.
          Thanks for your comments!

          Reply

Leave a Comment