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".
Drop it in the players hand in the hierarchy. Can be crude but it works (until you accidently destroy the terrain or something).
Answer by NextVertex · Apr 12, 2017 at 07:40 PM
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(){ myWeaponPrefab.setActive(false); }
You need to just let one weapon active with:
myWeaponPrefab.setActive(true);
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,
myWeaponList[0].setActive(false);
}
Hope it will help you
Your answer
Follow this Question
Related Questions
need help with a sway and recoil script for a weapon. 0 Answers
I need Drop Weapon Scripe 0 Answers
Photon Weapon View 1 Answer
Want to Make Mesh with script in an Egg Shape 0 Answers
How to run a script twice in a scene 0 Answers