Unity Attach Weapon to Character

UnityTagUnity Attach Weapon to Character

In the following tutorial i will guide you through the process of attaching a weapon to a character using unity engine with a little bit of c# coding.

Ready to start , lets go , fire up unity and make sure you have a basic character with the desired weapon to attach make sure both of them are visible in the scene

In this tutorial we assume that your character is already rigged and has a valid bone structure , we need that to specify where the weapon is going to be attached , for more information regarding character bones and rigging .

Regardless if your not using a humanoid character you can just attach the weapon to any sub-mesh or even the original mesh itself,

WeaponAttachment

Once you get everything setup you need to create a small script to attach the weapon to the character , the script will have 3 states ,

  • Idle Position
  • Handheld Position
  • Shooting Position

You can add as many states as you wish but for this tutorial we are going to cover the three above , so lets get started

Create new script called WeaponAttachment and attach it to the player game object

CreateNewScript
To create new script just right click anywhere in the project window and then expand the create list and choose C# Script make sure to name the script “WeaponAttachment” , then all you have to do is to attach that script to the player game object ,

To do that select the player game object from the hierarchy window and from the inspector window on the right choose Add Component and use the search box to find the script WeaponAttachment

AttachScript

Once added to the player game object all you need to do is write a simple script to get everything setup , to edit the script find it in the project window and double click to open the script editor window.

EditiorWindow

You should have a similar window shown above , now your ready to start coding .

We are going to try and make this a general script , which allows the user to attach any type of weapon to the script and it should work as expected , to do that we will need a game object handler to assign the desired weapon through the editor or even using another script to do that add the following lines

 

Next we are going to check if there is a weapon attached and then set the weapon into the idle state which will instantiate the weapon and set its position and rotation.

 

 

Before you run the game make sure to attach a weapon to the WeaponAttachment script

AttachWeaponToCharacter

to do that select the play game object from the hierarchy window this will allow you to specify a value form Main Weapon Attachment variable , click the small black circle to select a weapon then use the search box in the Select Game Object window to find the desired weapon mesh , that’s it now your weapon should show up in the game and automatically set to idle state .

WeaponAttachedView

Great now we have a weapon attachment system that works , next we are going to build the handled state , and the shooting state , don’t worry it going to be a simple task

 

Time to update the code by adding some bits to manage character state , first make sure that you have configured an animator since we are going to use this to find which state we are in and update the weapon position you may follow this tutorial to learn more about animator.

Add new function called UpdateWeaponState and call it every frame using the update function

Now we need to get the animator controller store it in a variable and get the current state of the player once we have the state we can change the weapon position and rotate accordingly , take look at the updated code

You can see that we are using if statement to find the current state and we are checking between “Running” and “Idle” state , once final thing to do is to add the shooting state and we should be done for this tutorial .

 

Finally we check if the shooting trigger is on and set the weapon position and rotation ,

 

Now you have a fully working weapon attachment system , there is still alot to be done in order to fix and improve some of the scripts keep in mind that the purpose of this tutorial is to get you started .

 

Hope you enjoyed this as much as i did , feel free to post comments i will gladly answer your questions .