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

Share

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

In this tutorial you will learn how to attach object to a Unity character and how to make a character wield a weapon. I am doing this simple item attach tutorial because in many games you are often displaying characters and enemies wielding items and weapons, and almost every game nowadays offers some kind of inventory system with switchable weapons.

In this Unity attach object to player tutorial we will use a character model created with Makehuman which i added some walk and attack animations. We will make spawn a sword weapon and as our character passes near to to it we will make him wield the sword in his hands, ready for attacking.

You will be able to download the full Unity attach object to player tutorial – How to wield a weapon unitypackage project file with C# code scripts near the end of this article.

Let’s start by introducing the game objects that will be featured in this unity attach object to player tutorial : we will make use of the 3d animated model of a male character created with makehuman that i built in my previous tutorial and for the item to attach to player we will be using a 3d sword model :

  1. Download the 3d animated model of the character from the following link , the file is a unitypackage that you have to import in a new empty unity project via Asset->Import Package->Custom Package.01-unity-attach-item-player-tutorial-parenting-wield-weapon
    3d animated  male character unitypackage free download link 

    The character model is already capable of handling player input (WASD to run around) via the mecanim character animator included script.
  2. Download and import in your unity project the 3d sword weapon model unitypackage file :02-unity-attach-item-player-tutorial-parenting-wield-weapon 3d weapon sword textured model unitypackage free download link
  3. Once you downloaded and imported the unitypackage files of the character and of the weapon you should find your Project panel filled with folders and files we will use trough this unity attach object to player tutorial.03-unity-attach-item-player-tutorial-parenting-wield-weapon

Now that we imported the package successfully we can start to build the scene where this unity attach item to player tutorial will take place.

Building the scene environment

We just need a basic plane where our character can move, so just create a plane in 0,0,0 and expand it as you like, then add some light sources to make things visbles.

  1. Create a plane and reset its transform component to 0,0,0, then add a directional light to light up the scene.
  2. Drag and drop the Player prefab into the scene, make sure to put the player just above the plane because it is not affected by gravity and will move leveled to the y position where you put it.
  3. Drag and drop the Sword prefab into the scene and put it distanced by the player. Scale it down to match the player model scale, i used (0.5,0.5,0.5).
  4. If you want you can parent the Main Camera to the player to make the view follow the player model.
  5. Save the scene as main as we will go trough the next part in this unity attach object to player tutorial, if you click play you can move the player with wasd controls.04-unity-attach-item-player-tutorial-parenting-wield-weapon

7 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!

    • 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.

  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!

    • 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.

        • 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!

Leave a Comment