• 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
0
Question by bpears · Jan 05, 2013 at 04:53 AM · java

GetButtonDown stuck in a loop? javascript included

The weapon switch is in an infinite loop when the button is pressed.. only suppose to switch to secondary weapon.

 if (Input.GetButtonDown("Switch") > 0 && canSwitch){
         weaponToSelect++;
     if (weaponToSelect > (weaponsInUse.length - 1)){
         weaponToSelect = 0;
     }
     DeselectWeapon();
 }
 
 if (Input.GetButtonDown("Switch") < 0 && canSwitch){
     weaponToSelect--;
     if (weaponToSelect < 0){
         weaponToSelect = weaponsInUse.length - 1;
     }
     DeselectWeapon();
 }



Comment
Add comment · Show 4
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 Landern · Jan 05, 2013 at 05:42 AM 1
Share

............ Input.GetButtonDown(...) returns type of boolean, just to start with, second is switch suppose to be triggers or bumpers, like, > 0 is suppose to the right and < 0 is suppose to be left?

avatar image bpears · Jan 05, 2013 at 06:14 AM 0
Share

actually i was going to use the "Y" button on xbox controller, so Joystick BUTTON 3.

avatar image Landern · Jan 05, 2013 at 06:28 AM 1
Share

You can use the Y(joystick button 3), but you can not use it as you indicated above, button 3 will either be down or not, so you can only check to see those states. This means that the weapon change logically will go one way. The trigger or bumpers seem more logical, one for increasing through your weapons, other for decreasing which is what you were trying to do. If switch is button 3, and you check to see if it is down, it either is, or isn't, it returns bool. With the axis it is using ranges as i stated in your other post.

avatar image bpears · Jan 05, 2013 at 06:46 AM 0
Share

What you said went over my head to be honest, but I kinda get what you're saying. I just don't know how I would change it.. any javascript book suggestions that pertain to unity?.. seems to be my reoccurring weakness.

EDIT: Figured it out! still would like to know of good book tho. Thanks!

1 Reply

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

Answer by Landern · Jan 05, 2013 at 05:47 AM

I'm going to just assume you are using the triggers on the controller. If you read the information from the community wiki, you will see that triggers are accessed by GetAxis not GetButtonDown.

GetAxis Indicates whether the left trigger is pressed when GetAxis("switch") returns 0 to -1 and the right trigger with 0 to 1. Please adjust your code to use GetAxis instead.

If you don't intend on marking answers correct i'm moving on.

Comment
Add comment · Show 1 · 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 bpears · Jan 05, 2013 at 06:14 AM 0
Share

I am not using triggers for this, I am using a button.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

I made my code so that if i hit an enemy its health will go down but when i hit the enemy the health doesnt go down. please help 1 Answer

How to go about purchasing objects with player score/points? 1 Answer

Sidescroller Character Movement Script 0 Answers

Tags Help scripting 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