- Home /
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:
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.
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
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.
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 ;)
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.