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 /
avatar image
4
Question by Leonard Kropf · Apr 17, 2010 at 09:53 AM · weapon

Weapon switching

i have this code from the fps toutorial

it works perfect

now i want to switch more than 2 weapons how can ideal with it

i tried many things but nothing worked

here is the script

function Start () { // Select the first weapon SelectWeapon(0); }

function Update () { // Did the user press fire? if (Input.GetButton ("Fire1")) BroadcastMessage("Fire"); if (Input.GetKeyDown("1")) { SelectWeapon(0); }
else if (Input.GetKeyDown("2")) { SelectWeapon(1); }
}

function SelectWeapon (index : int) { for (var i=0;i<transform.childCount;i++) {

// Activate the selected weapon

if (i == index)

transform.GetChild(i).gameObject.SetActiveRecursively(true);

// Deactivate all other weapons

else transform.GetChild(i).gameObject.SetActiveRecursively(false); } }

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

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by duck · Apr 17, 2010 at 10:17 AM

Well, from the code you've posted, it looks like you'd need to:

  • Add a new weapon as a child object of your player
    (Since the code assumes that each child of the player object is a weapon option, and this is how it accesses them)

  • Modify the 'if..else' part of you script so that instead of:

    if (Input.GetKeyDown("1")) 
    {
        SelectWeapon(0);
    }
    else if (Input.GetKeyDown("2")) 
    {
        SelectWeapon(1);
    }
    

    You add some more key options, like this:

        if (Input.GetKeyDown("1")) 
        {
            SelectWeapon(0);
        }
        else if (Input.GetKeyDown("2")) 
        {
            SelectWeapon(1);
        }
        else if (Input.GetKeyDown("3")) 
        {
            SelectWeapon(2);
        }
        else if (Input.GetKeyDown("4")) 
        {
            SelectWeapon(3);
        }
    

    If this doesn't work, post a more detailed description of the problems you're having with it, and I'll try to help further.

    Comment
    Add comment · Show 3 · 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 spinaljack · Apr 17, 2010 at 10:22 AM 0
    Share

    damn, too fast :P

    avatar image Leonard Kropf · Apr 17, 2010 at 05:30 PM 0
    Share

    hey super

    it works perfect

    avatar image duck ♦♦ · Apr 17, 2010 at 06:11 PM 0
    Share

    Cool :) Remember to accept and upvote the answer, if it worked for you.

    avatar image
    1

    Answer by Geoff 1 · Apr 17, 2010 at 03:25 PM

    Duck seems to be right, what you could also do, is just have 2 as go to the next weapon, 1 go back a weapon. Like this:

    var wepNum : float = 0;
    var numOfWeps : float = 5;
    //whatever the rest is
    function Update () {
    SelectWeapon(wepNum);
    }
    if(Input.GetKeyDown("1")) 
    {
    if(wepNum > 0) 
    wepNum--;
    }
    if(Input.GetKeyDown("2"))
    {
    if(wepNum < numOfWeps) {
    wepNum++;
    }
    //Rest of code required
    

    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
    avatar image
    0

    Answer by BlackHunter · Sep 11, 2012 at 07:12 PM

    if (Input.GetKeyDown("1")) { SelectWeapon(0); } else if (Input.GetKeyDown("2")) { SelectWeapon(1); } else if (Input.GetKeyDown("3")) { SelectWeapon(2); } else if (Input.GetKeyDown("4")) { SelectWeapon(3); } else if (Input.GetKeyDown("6")) { SelectWeapon(5); }

    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
    avatar image
    0

    Answer by AskMealirko321 · Jun 04, 2013 at 11:07 PM

    Here Is A Script To Switch To 5 Weapons Don't Assign Ticks :D I Recommend You 1-Knife.2-Pistol.3-Assault Rifle.4-Machine Gun.5-Sniper :D :D GOOD LUCK X_X

     var tick1 : AudioClip;
     var tick2 : AudioClip;
     var tick3 : AudioClip;
     var tick4 : AudioClip;
     var tick5 : AudioClip;
     
     
     function Start () {
        // Select the first weapon
        SelectWeapon(0);
     }
     
     function Update () {
        // Did the user press fire?
        if (Input.GetButton ("Fire1"))
           BroadcastMessage("Fire");
        
        if (Input.GetKeyDown("1")) {
           audio.PlayOneShot(tick1);
           SelectWeapon(0);
        }   
        else if (Input.GetKeyDown("2")) {
           audio.PlayOneShot(tick2);
           SelectWeapon(1);
        }
        else if (Input.GetKeyDown("3")) {
           audio.PlayOneShot(tick3);
           SelectWeapon(2);
        }
        else if (Input.GetKeyDown("4")) {
           audio.PlayOneShot(tick4);
           SelectWeapon(3);
        }
        else if (Input.GetKeyDown("5")) {
           audio.PlayOneShot(tick5);
           SelectWeapon(4);
        }   
           
     }
     
     function SelectWeapon (index : int) {
        for (var i=0;i
    
    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

    3 People are following this question.

    avatar image avatar image avatar image

    Related Questions

    gun switch script 26-feb-2010 1 Answer

    In-Game Weapon store 3 Answers

    Weaponbob for first person shooter 1 Answer

    Is there a way to make a selected object a child of another object during the game? 1 Answer

    bow and arrow (load and fire) 1 Answer


    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