How do you write a script for swinging a sword or just any weapon?

This is the script I wrote for attempting to make the sword swing. I have an animation and everything. The animation is called “SwingSword”.

There are many possibilities to do this, for example :

  • place all your weapon Prefabs in the player’s hierarchy (usualy in player’s hand hierarchy) and unactive them with :

    public GameObject myWeaponPrefab;
    void Start(){

You need to just let one weapon active with:


You can active a weapon prefab gameobject when the player collide with it (for example on the ground), and set the weapon which was active to unactive state.

But i suggest you , if you know a bit of scripting, to use a list/array, and to instantiate each weapon prefab from it.
You could then active/unactive weapon with the index they have in the list.
For example, if you put the knife at the first position in the list (in the inspector), the index of this knife will be 0, so you will be able to unactive it with this code :

//public GameObject[] myWeaponList;
//you should already wrote it at the top of your script 

void UnactiveAWeapon(){
//if your weapon is the knife,

Hope it will help you