• 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 MrRandomFella · Jul 18, 2014 at 07:48 AM · buttonchangelimitcounterpress

Limiting the amount of times a button can be pressed?

Hi, I am trying to recreate the old arcade game "Tempest". I have several objects placed in the lanes of the environment. My script counts how many times a button is pressed, each time the "Move Left" key is pressed; my counter takes away 1, disables the origin object's renderer and enables the renderer on the duplicate to the left. T$$anonymous$$s gives a 'lane changing' effect. The only problem I have is that once the player has 'moved' to the left or right edge, pressing the movement buttons will add extra numbers, keeping the player 'stuck' on the side until the opposite button is pressed enough times to move the other way.

So is there any possible way of "restricting" the amount of times that the movement buttons are able to be pressed. E.g. MIN = -5 (Pressing Left) MAX = 5 (Pressing Right).

SCRIPT:

 var curSteps : int;
 var meshOrigin : GameObject;
 var meshNeg1 : GameObject;
 var meshNeg2 : GameObject;
 var meshNeg3 : GameObject;
 var meshPos1 : GameObject;
 var meshPos2 : GameObject;
 var meshPos3 : GameObject;
 
 meshNeg1.renderer.enabled = false;
 meshNeg2.renderer.enabled = false;
 meshNeg3.renderer.enabled = false;
 meshPos1.renderer.enabled = false;
 meshPos2.renderer.enabled = false;
 meshPos3.renderer.enabled = false;
  
 function Update () {
     if (Input.GetKeyDown(KeyCode.A)){
     print ("Pressed A");
         curSteps -= 1;
     }
     if (Input.GetKeyDown(KeyCode.D)){
     print ("Pressed D");
         curSteps += 1;
     }
     if (curSteps == 0){
         curSteps = 0;
         meshOrigin.renderer.enabled = true;
         meshPos1.renderer.enabled = false;
         meshNeg1.renderer.enabled = false;
     }
     if (curSteps == 1){
         curSteps = 1;
         meshPos1.renderer.enabled = true;
         meshPos2.renderer.enabled = false;
         meshOrigin.renderer.enabled = false;
     }
     if (curSteps == 2){
         curSteps = 2;
         meshPos2.renderer.enabled = true;
         meshPos3.renderer.enabled = false;
         meshPos1.renderer.enabled = false;
     }
     if (curSteps == 3){
         curSteps = 3;
         meshPos3.renderer.enabled = true;
         meshPos2.renderer.enabled = false;
     }
     if (curSteps == -1){
         curSteps = -1;
         meshNeg1.renderer.enabled = true;
         meshNeg2.renderer.enabled = false;
         meshOrigin.renderer.enabled = false;
     }
     if (curSteps == -2){
         curSteps = -2;
         meshNeg2.renderer.enabled = true;
         meshNeg3.renderer.enabled = false;
         meshNeg1.renderer.enabled = false;
     }
     if (curSteps == -3){
         curSteps = -3;
         meshNeg3.renderer.enabled = true;
         meshNeg2.renderer.enabled = false;
     }
 }

DIAGRAM:

alt text

howitshouldwork.png (18.6 kB)
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

1 Reply

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

Answer by Tehnique · Jul 18, 2014 at 07:56 AM

For your problem: just add an int named "laneNumber" (that contains the number of lanes) and another called "currentLane" (the number of the lane the player is on). Every time you move the player update "currentLane". Before moving the player, check if "currentLane < laneNumber && currentLane > 0". If the condition is true move the player and update your other stuff, if not, don't do anyt$$anonymous$$ng.

Also, is enabling and disabling objects the best way to do t$$anonymous$$s? Why not actually move the object to the other lane? It's much easier to manage, and it jsut makes more sense. Look into translate. You just have to know the distance from the middle of a lane to the next one. You would use the same logic as above (laneNumber & currentLane), but instead of enable/disable, you would use translate to move the only object.

If you really want to do t$$anonymous$$s with enabling/disabling objects, just put all of them in a List and update them with loops, it's much easier, and you don't have to write so much code.

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 MrRandomFella · Jul 18, 2014 at 11:29 AM 0
Share
avatar image Tehnique · Jul 18, 2014 at 11:32 AM 0
Share
avatar image MrRandomFella · Jul 18, 2014 at 11:37 AM 0
Share
avatar image Tehnique · Jul 18, 2014 at 11:45 AM 0
Share

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

22 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

Related Questions

Change Materials On Button Press or Hold? 1 Answer

Cursor appearance change when mouse over a GUI button 2 Answers

Move camera upon button press 0 Answers

OnGUI: += and -= operations problem 2 Answers

gui.button down 2 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