Switching Weapon Script
hey I was wondering how to make a switching script for weapons...I have looked at some of the scripts but I need one that is used on a warship... :p the warship has a deck gun and missiles
I would like to know how to write a weapon switching script that will work for this that will switch to missiles( secondary) when I press 2 and switch to the deck gun when I press 1.
I know little to nothing about scripting so I am asking you
Answer by OctoMan · Oct 25, 2015 at 12:16 PM
Like in World Of Warships?
Let's say both guns have their own script on them, where they shoot, rotate, follow mouse etc..
What you need to do is, deactivate and activate those scripts on buttonpress on your needs.
Or just set a bool to true or false to which ever gun should be activated/deactivated.
How did you build up the firing?
I have a script called RTC TankGunController which shoots and that's what I have for both weapons. the missile uses the air plane script form unity to fly and crash into the ships (like a TV missile ) the script is form a tank physics builder asset that I bought.
If both weapons in one script and both weapons have their own script which you execute with the left mousebutton you can do it like:
public int weaponToActivate = 1;
// Update is called once per frame
void Update ()
{
if (Input.Get$$anonymous$$eyDown ("1"))
{
weaponToActivate = 1;
}
if (Input.Get$$anonymous$$eyDown ("2"))
{
weaponToActivate = 2;
}
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
if(weaponToActivate == 1)
{
Weapon1Attack();
}
else if (weaponToActivate == 2)
{
Weapon2Attack();
}
}
}
So you initiate weaponToactivate to 1. So the first gun is active always at start.
Then you switch the int to whatever gun number. So if you'll have more guns later on you can just use more numbers.
Finally if the use make an inpute leike with the left mousebutton, you check which number the int has, and depending on that number you call the script for the corresponding weapon.
Hope that makes sense, and is what you are looking for.
Sort of but both weapons use the same script for firing. I had found this script before you gave me this
using UnityEngine; using System.Collections;
public class Weapon : $$anonymous$$onoBehaviour {
public GameObject[] weapons; // push your prefabs
public int currentWeapon = 0;
private int nrWeapons;
void Start() {
nrWeapons = weapons.Length;
SwitchWeapon(currentWeapon); // Set default gun
}
void Update () {
for (int i=1; i <= nrWeapons; i++) {
if (Input.Get$$anonymous$$eyDown("" + i)) {
currentWeapon = i-1;
SwitchWeapon(currentWeapon);
}
}
}
void SwitchWeapon(int index) {
for (int i=0; i < nrWeapons; i++) {
if (i == index) {
weapons[i].gameObject.SetActive(true);
} else {
weapons[i].gameObject.SetActive(false);
}
}
}
}
so how would I make it disable the RTCtankguncontroller script on the one weapon and activate the other?
So you have your switching script already. In the SwitchWeapon Function you already set different weapons active or inactive. That mean you deactivate the whole gun with all components. You just need to drag in your guns in the inspector weapon array.
To answer your question
ins$$anonymous$$d of
weapons[i].gameObject.SetActive(true); you write weapons[i].gameObject.GetComponent ().enabled = true;
so it looks like that:
void SwitchWeapon(int index) {
for (int i=0; i < nrWeapons; i++) {
if (i == index) {
//weapons[i].gameObject.SetActive(true);
weapons[i].gameObject.GetComponent<RTCtankguncontroller> ().enabled = true;
} else {
//weapons[i].gameObject.SetActive(false);
weapons[i].gameObject.GetComponent<RTCtankguncontroller> ().enabled = false;
}
}
}
Answer by Delta6 · Oct 25, 2015 at 01:38 PM
so how would I build the script to switch weapons...Could you show me how to do it?
Your answer
Follow this Question
Related Questions
script not workig 0 Answers
need script for launching missile 0 Answers
Basic Enemy follow script for Unity five, desperately needed! 0 Answers
error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `=' 1 Answer
How to make it so that when quit menu is clicked then quit menu canvas pops up? 0 Answers