Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by DigitalDogfight · Jul 28, 2010 at 12:15 AM · switchkeyweaponssingle

Cycle weapons with only ONE key press (spacebar)

Trying to use the spacebar ONLY to cycle through 4 weapons.

The following script seems to be pretty straight forward but is stuck on SelectWeapon(0) at runtime. Yes, it's a tweeked version of the standard FPS tutorial script.

Any thoughts are appreciated.

function Start () { SelectWeapon (0); }

function Update () {

 if (Input.GetButton ("Fire1"))
     BroadcastMessage("Fire");

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

}

function SelectWeapon (index : int) { for (var i=0;i<transform.childCount;i++)
{ if (i == index) transform.GetChild(i).gameObject.SetActiveRecursively(true); else transform.GetChild(i).gameObject.SetActiveRecursively(false); } }

Comment
Add comment · Show 1
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 · Jul 28, 2010 at 12:51 AM 0
Share

What's the question?

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by 3dDude · Jul 28, 2010 at 01:01 AM

this will work :)

private var selectedWeapon : int = 0; function Start () { SelectWeapon (0); }

 function Update () 
 {

     if (Input.GetButton ("Fire1"))
         BroadcastMessage("Fire");

     if (Input.GetKeyDown("space")) 
     {
         selectedWeapon++;
         if(selectedWeapon == transform.childCount){
         selectedWeapon = 0;
         }
         SelectWeapon (selectedWeapon);

     }
  }

 function SelectWeapon (index : int) 
 {
     for (var i=0;i&lt;transform.childCount;i++)    
     {
         if (i == index){
             transform.GetChild(i).gameObject.SetActiveRecursively(true);
         }
         else{
             transform.GetChild(i).gameObject.SetActiveRecursively(false);
         }
     }
 }

hope this works! :D

Comment
Add comment · Show 4 · 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 DigitalDogfight · Jul 28, 2010 at 01:20 AM 0
Share

I had the notion from the start that this could be done with one IF statement for Get$$anonymous$$eyDown, including the selectedWeapon++; part! The bits of code you gave me was exactly what I needed, the final pieces to my maddening puzzle! WOR$$anonymous$$ED EXACTLY AS NEEDED, THAN$$anonymous$$ YOU SO $$anonymous$$UCH!!!

avatar image 3dDude · Jul 28, 2010 at 01:32 AM 0
Share

lol happy to help :)

avatar image sunnykhan4u21 · Jan 13, 2011 at 02:22 AM 0
Share

Thanx a $$anonymous$$ILLION

avatar image jesusluvsyooh · Apr 10, 2012 at 07:43 PM 0
Share

still works perfectly, thanks, just what i was looking for :D

avatar image
0

Answer by ADITYA GOUR · Sep 17, 2012 at 06:44 PM

hello Guyss!!. I copy the same script in my game, it works well but the shooting, switching, weapon realoding is very fast like 100times of normal working. when i check weapons individually then they work well. i think your script has need of 'Time.deltaTime' function.. I m a noob in programming , I don't know where it will be put.... in the script ....or any other solution.Anybody help me to fix this problem. i m waiting for your answer... and Thanx in advance... sorry for the weak grammar.

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

1 Person is following this question.

avatar image

Related Questions

Switch from one player to another. 5 Answers

I'm trying to make a weapon pickup script but can't quite get it to work. 0 Answers

Switching Between Weapons? 1 Answer

switch weapons script not working? 2 Answers

How to switch between 2d characters? 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