Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Delta6 · Oct 25, 2015 at 12:09 PM · scriptingbasicsshipweaponchanging

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Delta6 · Oct 25, 2015 at 01:35 PM 0
Share

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.

avatar image OctoMan · Oct 25, 2015 at 06:06 PM 0
Share

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.

avatar image Delta6 OctoMan · Oct 25, 2015 at 06:17 PM 0
Share

Sort of but both weapons use the same script for firing. I had found this script before you gave me this

avatar image Delta6 OctoMan · Oct 25, 2015 at 06:17 PM 0
Share

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);
         }
     }
 }
 

}

avatar image Delta6 OctoMan · Oct 25, 2015 at 06:22 PM 0
Share

so how would I make it disable the RTCtankguncontroller script on the one weapon and activate the other?

avatar image OctoMan Delta6 · Oct 25, 2015 at 06:45 PM 0
Share

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;
          }
      }
  }


Show more comments
Show more comments
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges