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 :
- 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.
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.
- Download and import in your unity project the 3d sword weapon model unitypackage file : 3d weapon sword textured model unitypackage free download link
- 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.
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.
- Create a plane and reset its transform component to 0,0,0, then add a directional light to light up the scene.
- 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.
- 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).
- If you want you can parent the Main Camera to the player to make the view follow the player model.
- 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.