Quantcast
Channel: Answers by "Hachley"
Viewing all articles
Browse latest Browse all 36

Answer by Hachley

$
0
0
I wouldn't suggest you spawn prefabs every time you switch weapons, instead you could have the weapons as children of the player's body,already positioned perfectly, and just have them activated/deactivated whenever you need them. Not only is this more easier to code, but also easier to test every aspect of the weapon, and is pretty much the right way. At the start of the game you could have all of the weapons deactivated except one, afterwards using just this code: var weapon01 : GameObject; //drag the first child in here via the inspector var weapon02 : GameObject; //and the second one here function Update () { //weapon01 if(Input.GetKeyDown(KeyCode.Alpha1)) { //if presses "1" equipWeapon01(); //call weapon1 func } //weapon02 if(Input.GetKeyDown(KeyCode.Alpha2)) { //if presses "2" equipWeapon02(); //call weapon2 func } } function equipWeapon01() { //deactivates all weapons except first one weapon01.SetActive(true); weapon02.SetActive(false); } function equipWeapon02() { //deactivates all except the second one weapon01.SetActive(false); weapon02.SetActive(true); } And to swap between them two using "Q"(which works fine if you have only 2 weapons in game), you could do something like this: function Update () { if(Input.GetKeyDown(KeyCode.Q)) { swapWeapons(); } } function swapWeapons() { if(weapon01.active == true) { weapon01.SetActiveRecursively(false); weapon02.SetActiveRecursively(true); } else { weapon02.SetActiveRecursively(false); weapon01.SetActiveRecursively(true); } }

Viewing all articles
Browse latest Browse all 36

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>