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
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. This 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 anything.

Also, is enabling and disabling objects the best way to do this? 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 this 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

Sorry about the late response, I need the player to "snap" to each lane. $$anonymous$$y script may be long but It needs to be suitable for levels that come in a variety of shapes rather than just a flat plane. Just trying to save time making varying levels :P

avatar image Tehnique · Jul 18, 2014 at 11:32 AM 0
Share

Well, then just set transform.position. The correct way to set it is with a new Vector, like "transform.position = new Vector3(x,y,z);".

That will make the object "snap" to the new coordinates.

avatar image MrRandomFella · Jul 18, 2014 at 11:37 AM 0
Share

Thanks Tehnique,just solved my issue with

if (curSteps <= -3){ curSteps = -3; } if (curSteps >= 3){ curSteps = 3; }

I will make a second version of this script using transform.position and see what works best. Btw, thanks for the help ;)

avatar image Tehnique · Jul 18, 2014 at 11:45 AM 0
Share

You're welcome! Try to simplify the code and use position, it's the normal way to do it, less resource intensive and cleaner code/logic.

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

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

Changing the sliding door script in Angry Bots to open at a button press. 0 Answers

Cursor appearance change when mouse over a GUI button 2 Answers

Move camera upon button press 0 Answers

Action executed with more than one key? 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